LOG(): The logarithm of a number to base

Calculate a logarithm

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.

LOG() with Base

<?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
?>
For real-valued logarithms, $num should be positive. A custom base must be greater than zero and must not equal 1.

Example: Calculating Logarithms with Different Bases

$value = 100;
$base = 2;
$log_base2 = log($value, $base);  // Output: 6.643856
echo $log_base2;

Example: Handling Invalid Inputs

$value = -10;
$result = log($value);
if (is_nan($result)) {
    echo "Logarithm undefined for negative values.";
}
Output
Logarithm undefined for negative values.

Questions


Math Functions LOG10() function LOG1P() function


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer