log
Returns the natural logarithm of a number, or its logarithm in any base you supply.
log(float $num, float $base = M_E): float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | float | The value to calculate the logarithm for. |
$baseoptional | float= M_E | The optional logarithmic base to use. Defaults to M_E, Euler's number, giving the natural logarithm. |
Return value
floatThe logarithm of num to base, if given, or the natural logarithm of num.
Errors
Throws a ValueError when base is less than or equal to 0. A base of exactly 1 returns NAN rather than throwing.
log() returns the natural logarithm — the power you would raise e to in order to get $num. Pass a second argument to use a different base.
When to reach for it
Logarithms turn multiplication into addition, which is why they appear in growth and decay problems ("how many periods until this doubles?"), in information theory (entropy, bits of surprise), on log-scale chart axes, and in any pipeline that multiplies many small probabilities together and would otherwise underflow to zero.
Bases and their edge cases
Base 2 and base 10 are special-cased internally to the C library's log2() and log10(), so they return exact results for exact powers. Any other base is computed as log($num) / log($base), which can land a digit or two away from the mathematically exact answer. A base of exactly 1 returns NAN, and a base of 0 or less throws a ValueError.
The rules for $num are IEEE, not exceptions: log(0) is -INF and log() of a negative number is NAN, both silently. Validate the input yourself if either would be a bug. When $num is very close to 1, use log1p() instead to keep your significant digits.
Example
When to use it
- Working out how many compounding periods it takes for a balance to double.
- Calculating Shannon entropy in bits with log($p, 2).
- Converting a linear value to a log-scaled chart position.
- Summing log-probabilities instead of multiplying probabilities that would underflow to zero.
Frequently asked questions
How do I calculate a logarithm with a specific base in PHP?
Pass the base as the second argument: log(8, 2) returns 3. Bases 2 and 10 are computed exactly by dedicated C functions, while other bases use log($num) / log($base) internally.
What is the difference between log and log10?
log() defaults to the natural logarithm with base e, while log10() is fixed at base 10. log($num, 10) and log10($num) return identical results.
What does log(0) return in PHP?
-INF, with no warning and no exception. log() of a negative number returns NAN. Check the input yourself if either case would be a bug in your code.
More examples
How long until it doubles?
Solving target = start * (1 + rate)^n for n gives log(target/start) / log(1 + rate). At 7% per period a value doubles in roughly 10.2 periods, and going from a thousand to a million takes about 102. The base cancels out in the ratio, so any base gives the same answer.
<?php
$rate = 0.07; // 7% growth per period
printf("doubling time: %.2f periods\n", log(2) / log(1 + $rate));
$users = 1000;
$target = 1000000;
printf("periods to target: %.1f\n", log($target / $users) / log(1 + $rate));Base 2 and base 10 are exact
For bases 2 and 10, log() delegates to the platform's log2() and log10(), which return clean integers for exact powers — 0, 1, 3, 10 and 12 here. The last two lines print the same quantity computed both ways; the change-of-base route can differ in the final digits because it performs two roundings instead of one.
<?php
foreach ([1, 2, 8, 1024, 4096] as $n) {
printf("log2(%-5d) = %s\n", $n, log($n, 2));
}
printf("log(8, 2) = %.17f\n", log(8, 2));
printf("log(8) / log(2) = %.17f\n", log(8) / log(2));Invalid bases
The three invalid bases are handled two different ways. A base of 1 is mathematically undefined and comes back as NAN with no exception, so it will slip through unnoticed. A base of 0 or below throws a ValueError naming argument #2, which is loud and catchable.
<?php
var_dump(log(100, 1)); // NAN: base 1 is undefined
try {
log(100, 0);
} catch (ValueError $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
}
try {
log(100, -2);
} catch (ValueError $e) {
echo "negative base rejected\n";
}
Discussion