log()
Today, we will discuss the log() function in PHP. This function is used to calculate the natural logarithm of a number.
The log() function in PHP returns the logarithm of a number. By default it returns the natural logarithm — the logarithm to base e (Euler's number, approximately 2.71828) — but you can pass a second argument to compute the logarithm to any base.
A logarithm answers the question "to what power must the base be raised to produce this number?" For example, log(8, 2) is 3, because 2 raised to the power of 3 equals 8. This page covers the syntax, common bases, edge-case behavior, and a practical use case.
Syntax
log(float $num, float $base = M_E): float| Parameter | Description |
|---|---|
$num | The number whose logarithm you want. Must be positive for a real result. |
$base | Optional. The logarithmic base. Defaults to M_E (≈ 2.71828), giving the natural logarithm. |
The function returns a float: the logarithm of $num to the given base.
Basic example: natural logarithm
When you call log() with a single argument, you get the natural logarithm (base e):
Using a custom base
Pass a second argument to compute the logarithm to a different base. This is handy for base-2 (binary) or base-10 (common) logarithms:
<?php
echo log(100, 10), "\n"; // 2 (10^2 = 100)
echo log(8, 2), "\n"; // 3 (2^3 = 8)
echo log(1, 5), "\n"; // 0 (any base^0 = 1)
?>For base-10 specifically, PHP also offers the dedicated log10() function, and for small values near 1 there is log1p(), which is more accurate than log(1 + x).
Edge cases to watch for
The mathematical logarithm is only defined for positive numbers, so log() returns special float values outside that range:
<?php
echo log(1), "\n"; // 0 — log of 1 is always 0
echo log(0), "\n"; // -INF — log of 0 is negative infinity
echo log(-5), "\n"; // NAN — log of a negative number is "not a number"
?>Because NAN and INF propagate silently through arithmetic, validate input before calling log():
<?php
$value = -5;
if ($value > 0) {
echo log($value);
} else {
echo "log() requires a positive number";
}
// Outputs: log() requires a positive number
?>You can detect these results with is_nan() and is_infinite() if a value reaches log() unchecked.
A practical use case
Logarithms are common when you need to know how many digits a number has, or to scale data. For example, the number of decimal digits in a positive integer is floor(log10($n)) + 1:
<?php
$n = 12345;
$digits = (int) floor(log10($n)) + 1;
echo "{$n} has {$digits} digits"; // Outputs: 12345 has 5 digits
?>Related functions
log10()— base-10 logarithm.log1p()— accuratelog(1 + x)for smallx.exp()— the inverse of the natural log (eraised to a power).pow()— raise a number to a power.sqrt()— square root.- PHP Math Functions — overview of all math helpers.
Conclusion
The log() function is a reliable tool for logarithmic calculations in PHP. With its optional second argument it handles any base, while its natural-log default covers the most common need. Remember the edge cases — log(0) is -INF and log() of a negative number is NAN — and validate input to keep your calculations robust.