log10()
Today, we will discuss the log10() function in PHP. This function is used to calculate the base-10 logarithm of a number.
The log10() function returns the base-10 logarithm of a number — that is, the power to which 10 must be raised to produce that number. Because log10(1000) is 3 (since 10³ = 1000), it is the natural tool for working with orders of magnitude: decibels, the pH scale, the Richter scale, and "how many digits does this number have."
This page covers the syntax, edge cases (zero, negatives, very small numbers), how it differs from log(), and a couple of real formulas you can drop into code.
Syntax
log10(float $num): float$num— the value whose base-10 logarithm you want. It should be a positive number.- Return value — the base-10 logarithm of
$numas afloat.
The defining relationship is simple: if log10($x) is $y, then pow(10, $y) is $x.
Basic example
log10(100) is 2 because 10² = 100. Likewise log10(1000) is 3 and log10(1) is 0, since any number raised to the power 0 equals 1.
A range of values
The result grows by exactly 1 every time the input is multiplied by 10, and goes negative for inputs below 1:
<?php
echo log10(1000) . "\n"; // 3
echo log10(100) . "\n"; // 2
echo log10(10) . "\n"; // 1
echo log10(1) . "\n"; // 0
echo log10(0.1) . "\n"; // -1
echo log10(0.001) . "\n"; // -3
echo log10(50) . "\n"; // 1.6989700043360187
?>Note that log10(50) is between 1 and 2, because 50 sits between 10¹ and 10².
Edge cases: zero and negative numbers
The logarithm of 0 is undefined (you can never reach 0 by raising 10 to any power), and logarithms of negative numbers are not real. PHP signals these with special float values instead of throwing:
<?php
var_dump(log10(0)); // float(-INF)
var_dump(log10(-5)); // float(NAN)
?>Guard against them when the input is user-supplied:
<?php
function safeLog10($n) {
if ($n <= 0) {
return null; // log10 is only defined for positive numbers
}
return log10($n);
}
var_dump(safeLog10(100)); // float(2)
var_dump(safeLog10(0)); // NULL
?>You can also test a result with is_nan() and is_infinite() if you prefer to call log10() first and inspect afterwards.
log10() vs. log()
Both functions compute logarithms, but with different bases:
log10($x)— always base 10. Equivalent tolog($x, 10).log($x)— base e (the natural logarithm, ≈ 2.718) when called with one argument.log($x, $base)— logarithm to any base you pass.
So log10($x) and log($x, 10) return the same value; log10() is just a faster, clearer shorthand for the common base-10 case. For base-2 logarithms, use log($x, 2). See log() for the general-purpose version and log1p() for accurate logarithms of values close to 1.
Practical use: counting digits
A common trick: the number of digits in a positive integer is floor(log10($n)) + 1.
<?php
function digitCount($n) {
return (int) floor(log10($n)) + 1;
}
echo digitCount(7) . "\n"; // 1
echo digitCount(100) . "\n"; // 3
echo digitCount(99999) . "\n"; // 5
?>This works because log10() tells you the order of magnitude directly.
Conclusion
log10() returns the base-10 logarithm of a number — the exponent 10 needs to reach that value. Remember three things: the result increases by 1 for every ×10 in the input, the input must be positive (0 gives -INF, negatives give NAN), and log10($x) is identical to log($x, 10). For other bases reach for log(), and explore the full set of math helpers in PHP Math.