How to Use log() Function in PHP

Logarithms are mathematical functions that help us make sense of large numbers and exponents. In PHP, we have two essential log functions: natural log and log10. The natural logarithm uses the Euler number “e” as its base, while log10 employs the base 10. These functions provide a convenient way to calculate logarithmic values in PHP and find solutions to various problems.

This guide will provide you a detail of the log() function in PHP with examples.

PHP log() Function

The log() function is a built-in PHP function that allows you to calculate the natural log of different numbers. When using this function in PHP, you have the option to pass two arguments; the first one is the “number” that is used to specify the value for which you want to calculate the logarithm. The second argument is the “base” which is not mandatory but can be used to determine the base of the logarithm. If you skip the base argument, the function will assume the natural logarithm with base “e” by default.

To use log() function in PHP, you must follow the below-given syntax:

log(number, base);

The above function returns the natural log of a number.

Use log() Function in PHP

Here are a few examples that describe the use of the log() function in PHP.

Example 1

In the following example, we are using the log() function to calculate the natural log of a specific number.

$number = 4;
echo("The log of 4 is: " .log($number));
echo "\n";
echo("The log of 1 is: " .log(1));

Example 2

You can also pass the base number in the log() function. The following sample code snippet explains the use of log() to find the logarithm of a provided number with a specific base:

$number = readline("Enter a number: ");
$base = readline("Enter the base: ");
echo "The log of $number with base $base is: " . log($number, $base);

Use log10() Function in PHP

The log10() function in PHP is a pre-defined math function that returns the base 10 logarithm of the entered number. The following is the syntax for using log10 in PHP.


It accepts one compulsory parameter, a number to find the logarithm for.


The log10 of a number can be calculated in PHP using the following code:

$number = readline("Enter a number: ");
echo "The log of $number is: " . log10($number);

Bottom Line

In PHP, the log() function is a helpful tool for developers who need to work with logarithmic functions. This function allows you to determine a number’s natural logarithm, which is helpful for a variety of online applications. You can also use the log10 () function of PHP to calculate the logarithm of base 10 in PHP.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.