log() returns the natural logarithm by default. Supply a second argument when you need another valid logarithm base.
echo log(M_E); // 1
echo log(8, 2); // 3
For positive inputs, omit the second argument for the natural logarithm or provide a valid base greater than zero and not equal to one.
<?Php
echo log(2);// Output is 0.69314718055995
echo "<br>";
echo log(-1);// Output is NAN
echo "<br>";
echo log(0);// Output is -INF
echo "<br>";
echo log(5.7);// Output is 1.7404661748405
?>
Syntax
float log(float $num, float $base = M_E)
$num is the value whose logarithm is required. Omit $base for the natural logarithm.
<?Php
echo log(5,2);// Output is 2.3219280948874
echo "<br>";
echo log(5,10);// Output is 0.69897000433602
echo "<br>";
echo log(5,2.7183);// Output is 1.609427153552
?>
$num should be positive. A custom base must be greater than zero and must not equal 1.$value = 100;
$base = 2;
$log_base2 = log($value, $base); // Output: 6.643856
echo $log_base2;
$value = -10;
$result = log($value);
if (is_nan($result)) {
echo "Logarithm undefined for negative values.";
}
Output
Logarithm undefined for negative values.
log() function in PHP?log() function used to calculate the natural logarithm of a number?log() function in PHP?e (natural logarithm) when using the log() function?log() function?log() return for zero or a negative input?log() function when it is successful?is_nan() and is_infinite() help check non-finite logarithm results?log() function be used to calculate the logarithm of complex numbers?Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.