PHP Function Reference

PHP log() Function



The PHP log() function returns the natural logarithm or logarithm to the specified base of a given number. In special cases it returns the following:

  • If any of the argument is NAN or first argument is less than zero, then the result is NAN.

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 example below, log() function is used to calculate the natural logarithm of a given number.

<?php
echo "log(0) = ".log(0)."\n";
echo "log(0.5) = ".log(0.5)."\n";
echo "log(1) = ".log(1)."\n";
echo "log(10) = ".log(10)."\n";
echo "log(100) = ".log(100)."\n";
echo "log(INF) = ".log(INF)."\n";
echo "log(-INF) = ".log(-INF)."\n";
echo "log(NAN) = ".log(NAN)."\n";
?>

The output of the above code will be:

log(0) = -INF
log(0.5) = -0.69314718055995
log(1) = 0
log(10) = 2.302585092994
log(100) = 4.6051701859881
log(INF) = INF
log(-INF) = NAN
log(NAN) = NAN

Example:

Consider one more example where log() function is used to calculate the logarithm to the specified base of a given number. Please note that the base must be a positive number.

<?php
//base 10
echo "log(0, 10) = ".log(0, 10)."\n";
echo "log(0.5, 10) = ".log(0.5, 10)."\n";
echo "log(1, 10) = ".log(1, 10)."\n";
echo "log(10, 10) = ".log(10, 10)."\n";
echo "log(100, 10) = ".log(100, 10)."\n";

//base 2
echo "\nlog(0, 2) = ".log(0, 2)."\n";
echo "log(0.5, 2) = ".log(0.5, 2)."\n";
echo "log(1, 2) = ".log(1, 2)."\n";
echo "log(16, 2) = ".log(16, 2)."\n";
echo "log(256, 2) = ".log(256, 2)."\n";
?>

The output of the above code will be:

log(0, 10) = -INF
log(0.5, 10) = -0.30102999566398
log(1, 10) = 0
log(10, 10) = 1
log(100, 10) = 2

log(0, 2) = -INF
log(0.5, 2) = -1
log(1, 2) = 0
log(16, 2) = 4
log(256, 2) = 8

❮ PHP Math Reference