Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


The PHP Math log() function returns the natural logarithm or logarithm to the specified base of a given number.

Syntax

log(number, base)

Parameters

number Required. Specify the number.
base Optional. Specify the base. Default is e

Return Value

Returns the natural logarithm or logarithm to the specified base of a given number.

Example:

In the below example, log() function is used to calculate the natural logarithm and logarithm to the specified base of a given number.

<?php
$x = 10; 
$y = 50;
$z = 100;

//calculate natural logarithm
echo "log($x) equal to: ".log($x)."\n";
echo "log($y) equal to: ".log($y)."\n";
echo "log($z) equal to: ".log($z)."\n";

echo "\n";
//calculate base-10 logarithm 
echo "log($x, 10) equal to: ".log($x, 10)."\n";
echo "log($y, 10) equal to: ".log($y, 10)."\n";
echo "log($z, 10) equal to: ".log($z, 10)."\n";
?>

The output of the above code will be:

log(10) equal to: 2.302585092994
log(50) equal to: 3.9120230054281
log(100) equal to: 4.6051701859881

log(10, 10) equal to: 1
log(50, 10) equal to: 1.698970004336
log(100, 10) equal to: 2

❮ PHP Math Functions