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:
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.
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.
Example
The log10 of a number can be calculated in PHP using the following code:
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.