Logarithm is the counter operation to exponentiation. The logarithm of a number is in fact the exponent to which the other number i.e. the base must be raised to produce that number. If the Euler’s Number ‘e’ is used as the base of any logarithmic operation it is then known as Natural Logarithmic Operation, another popular logarithmic operation is to take 10 as the base.
In PHP, the log() function is used to calculate the natural logarithm of a number if no base is specified and the log10() function calculates the base 10 logarithm of a number.
log() Function
Syntax:
float log ($arg, $base)
Parameters: The function can accept at most two parameters as follows:
- $arg: This is a required parameter that refers to the number of whose the logarithm is to be calculated.
- $base: This is an optional parameter that refers to the base of the logarithmic operation. If not given, M_E i.e. the Euler’s Number is used as Base to calculate the Natural Logarithm.
Return Type: This function returns the result of the logarithmic operation.
Examples:
Input : $arg = M_E * M_E; Output : 2 Input : $arg = 1024; $base = 2; Output : 10
Below program illustrates the working of log() in PHP:
<?php // PHP code to illustrate the working of log() Function $arg = 81; $base = 3; for (; $base <= $arg ; $base *= $base ) echo 'log(' . $arg . ', ' . $base . ') = ' .log( $arg , $base ). "\n" ; ?> |
Output:
log(81, 3) = 4 log(81, 9) = 2 log(81, 81) = 1
log10() Function
Syntax:
float log10 ($arg)
Parameters: The function accepts a single parameter $arg which refers to the number of whose the logarithm is to be calculated.
Return Type: This function returns the result of the Base 10 logarithmic operation.
Examples:
Input : $arg = 100; Output : 2 Input : $arg = 10000; $base = 4; Output : 10
Below program illustrates the working of log10() in PHP:
<?php // PHP code to illustrate the working of log10() Function $arg = 100000; for (; $arg >=10; $arg /=10) echo 'log10(' . $arg . ') = ' .log10( $arg ). "\n" ; ?> |
Output:
log10(100000) = 5 log10(10000) = 4 log10(1000) = 3 log10(100) = 2 log10(10) = 1
Important points to note:
- log() function is a very popular method to calculate logarithmic values.
- PHP | exp() function is the functional counterpart of log().