PHP log() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

The log () function calculates natural logarithm of a number.

Logarithm is inverse of eponential. If 102=100, it means log10100=2. Natural logarithm is calculated with Euler Number e as base. In PHP, the predefined constant M_E gives value of e which is 2.7182818284590452354

For example, exp(4.60517018599)=100 (it is also same as e4.60517018599=100). Hence, loge100=4.60517018599

In PHP, loge is represented by log() function

Syntax

log ( float $arg [, float $base = M_E ] ) : float

Parameters

Sr.NoParameter & Description
1arg
The value whose logarithm is to be calculated
2base
Default value of base is M_E.

Return Values

PHP log() function returns logarithm of arg to base. If base is not given, result is natural logarithm.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following example calculates natural logarithm of 100

<?php
   $arg=100;
   echo "log(" . $arg . ")=" . log($arg);
?>

Output

This will produce following result −

log(100)=4.6051701859881

Example

 Live Demo

Following code calculates natural logarithm of 2. The result is equal to a predefined constant M_LN2

<?php
   $arg=2;
   echo "log(" . $arg . ")=" . log($arg) . "\n";
   echo "predefined constant M_LN2 : " . M_LN2
?>

Output

This will produce following result −

log(2)=0.69314718055995
predefined constant M_LN2 : 0.69314718055995

Example

 Live Demo

Following example calculates log21024 and returns 10 −.

<?php
   $arg=1024;
   $base=2;
   echo "log(" . $arg . "," . $base . ")=" . log($arg, $base) . "\n";
?>

Output

This will produce following result −

log(1024,2)=10

Example

 Live Demo

Similarly log2e results in predefined constant M_LOG2E

<?php
   $arg=M_E;
   $base=2;
   echo "log(" . $arg . "," . $base . ")=" . log($arg, $base) . "\n";
   echo "predefined constant M_LOG2E = ". M_LOG2E;
?>

Output

This will produce following result −

log(2.718281828459,2)=1.442695040889
predefined constant M_LOG2E = 1.442695040889
raja
Published on 29-Jun-2020 12:38:16
Advertisements