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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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";
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to illustrate the working of log10() Function 
$arg = 100000;
for(;$arg>=10;$arg/=10)
  echo 'log10('.$arg.') = '.log10($arg)."\n";
?>

chevron_right


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().


My Personal Notes arrow_drop_up

FullStack Developer of six websites Verified Freelancer with 19 complete projects and 4.9 rating Modest Lifelong Learner

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.