PHP | log(), log10() Functions
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 log ($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().
Recommended Posts:
- PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)
- Difference between regular functions and arrow functions
- PHP | Functions
- PHP | String Functions
- PHP | die() & sleep() functions
- PHP | GMP Functions Complete Reference
- Array of functions in JavaScript
- PHP | startsWith() and endsWith() Functions
- jQuery | Callback Functions
- PHP | Ds\Map Functions Complete Reference
- PHP | Ds\Set Functions Complete Reference
- CSS | Functions Complete Reference
- How to add many functions in one ng-click directive?
- Arrow functions in JavaScript
- PHP | strrpos() and strripos() Functions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



