PHP log(), log10() Functions
Last Updated :
22 Jun, 2023
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
// 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
// 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().
Similar Reads
PHP log1p() Function The log1p() is an inbuilt PHP function that returns log(1+number), & when the value of the number is close to zero, then it will compute the way that will provide the accurate result. Syntax: log1p(float $num): floatParameter: This function accepts only a single parameter: num: This parameter sp
1 min read
PHP | Functions A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param
8 min read
PHP | localtime() Function The localtime() function is an inbuilt function in PHP which is used to return the local time. The array returned by the localtime() function is similar to the structure returned by the C function call. The $timestamp and $is_associative are sent as parameters to the localtime() function and it retu
2 min read
PHP list() Function The list() function is an inbuilt function in PHP which is used to assign array values to multiple variables at a time. This function will only work on numerical arrays. When the array is assigned to multiple values, then the first element in the array is assigned to the first variable, second to th
2 min read
PHP ob_get_length() Function The ob_get_length() function is an inbuilt function in PHP that is used to get the length of the current output buffer. The output buffer length is the number of bytes in the buffer. Syntax: ob_get_length(): int|falseParameters: This function does not accept any parameter. Return Values: The ob_get_
2 min read
PHP Math Functions PHP is a scripting language that comes with many built-in math functions and constants. These tools help you do basic math, solve more complex problems, and work with different math concepts. This guide gives an overview of PHP's math functions and constants, along with examples, practical uses, and
5 min read