Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
log() function in PHP
The log() function in PHP calculates the natural logarithm (base e) of a number. It can also calculate logarithms with a specified base when provided as the second parameter.
Syntax
log(number, base)
Parameters
number − The value for which you want to calculate the logarithm (must be positive)
base − Optional. The logarithmic base. If omitted, calculates natural logarithm (base e)
Return Value
Returns the logarithm of the number. Returns -INF for zero, NAN for negative numbers, and a float value for positive numbers.
Examples
Basic Natural Logarithm
Calculate the natural logarithm of 1 −
<?php
echo log(1);
?>
0
Logarithm of Zero
When the input is zero, the function returns negative infinity −
<?php
echo log(0);
?>
-INF
Multiple Values
Calculate logarithms of different numbers −
<?php
echo log(10) . "<br>";
echo log(2.7) . "<br>";
echo log(exp(1)); // e^1 = e, so log(e) = 1
?>
2.302585092994 0.99325177301028 1
Using Custom Base
Calculate logarithm with base 10 and base 2 −
<?php
echo log(100, 10) . "<br>"; // log base 10 of 100
echo log(8, 2) . "<br>"; // log base 2 of 8
echo log(27, 3); // log base 3 of 27
?>
2 3 3
Conclusion
The log() function is essential for mathematical calculations involving logarithms. Use it with one parameter for natural logarithms or two parameters for custom base logarithms.
