Signature
log10(float $num): floatPurpose
log10() answers the question: "10 raised to what power gives this number?" For 1000 the answer is 3, because 10³ = 1000. The function is useful whenever you care about orders of magnitude: counting digits, choosing a display scale, working with decibel-style formulas, or drawing a logarithmic axis.
It is available in all modern PHP versions and needs no extension.
Signature
Parameters
$numis the value to process. It has no default, so you must pass it. An integer is converted to a float automatically. A numeric string such as"100"is also accepted in the default coercive typing mode.
The mathematically meaningful input range is $num > 0. Other values do not raise an exception; they produce special float results, described below.
Return value and failure cases
The function always returns a float. Even when the result looks whole, such as 3, the type is float, and var_dump() shows float(3).
There is no false return and no warning for out-of-range input. Instead you get IEEE floating point special values:
log10(0)returns-INF, negative infinity.log10(-1)(any negative number) returnsNAN.log10(INF)returnsINF.
A value that cannot be interpreted as a number, such as the string "abc", causes a TypeError, as with other internal functions that take a float parameter.
Example 1: Powers of ten and a fractional result
Exact powers of ten give clean whole results, and other positive numbers give fractions. Use round() when you display a fractional logarithm.
var_dump(log10(1000));
var_dump(log10(1));
echo round(log10(2), 4), "\n";float(3) float(0) 0.301
Example 2: Counting the digits of a positive integer
For a positive integer n, the number of decimal digits is floor(log10(n)) + 1. This works because each additional digit raises the logarithm by one.
function digitCount(int $n): int
{
return (int) floor(log10($n)) + 1;
}
echo json_encode([digitCount(9), digitCount(10), digitCount(999), digitCount(1000), digitCount(123456)]);
echo "\n";[1,2,3,4,6]
This approach only fits n >= 1. For 0 the logarithm is -INF, and casting infinity to an integer is not a meaningful digit count. Guard that case before calling the function.
Example 3: Zero and negative input
Invalid mathematical input does not stop the script. You must check the result yourself if the data is untrusted.
var_dump(log10(0));
var_dump(log10(-1));
var_dump(is_nan(log10(-1)));float(-INF) float(NAN) bool(true)
Use is_nan() to detect NAN, because NAN == NAN is false. Use is_infinite() or is_finite() for the infinite case.
Edge cases
- Floating point precision. The result is a double, so values very close to a power of ten can land slightly on either side of a whole number. A digit counter built on
floor(log10($n))may therefore be unreliable for very large integers or for floats with rounding error. For exact digit counts of big integers,strlen((string) $n)is safer. - Values between 0 and 1. The result is negative. For example
log10(0.01)is-2. - Integer input. An
intis widened tofloat, so integers beyond 2⁵³ may already have lost precision before the logarithm is computed. - Comparing results. Avoid
==on computed logarithms. Compare with a small tolerance or round first.
Common mistakes
- Assuming an int is returned.
log10(100)isfloat(2), notint(2). Cast explicitly, or useintdiv-style logic elsewhere, if you need an integer. - Passing zero or negative values without checking. You receive
-INForNAN, which can silently spread through later arithmetic. - Using it for the natural logarithm.
log10()is base 10 only. Uselog()for base e, orlog($num, 10)if you prefer one function with an explicit base. - Trusting exact equality. Tests like
log10($x) == 3are fragile for computed$x. - Forgetting to round for display. Printing raw floats can show long, noisy decimals.
Related functions
log()– natural logarithm, or logarithm with a chosen base.log1p()– accuratelog(1 + x)for very smallx.exp()– the inverse of the natural logarithm.pow()or the**operator – raise 10 to a power, the inverse oflog10().floor(),round()– commonly paired withlog10().is_nan(),is_infinite(),is_finite()– check special results.
Recap
log10(float $num): float returns the base-10 logarithm of a number. Powers of ten give whole-looking floats, zero gives -INF, and negative input gives NAN. Validate input first, round for display, and remember that float precision limits exact digit-counting tricks.
Tested examples
var_dump(log10(1000));
var_dump(log10(1));
echo round(log10(2), 4), "\n";
float(3) float(0) 0.301
function digitCount(int $n): int
{
return (int) floor(log10($n)) + 1;
}
echo json_encode([digitCount(9), digitCount(10), digitCount(999), digitCount(1000), digitCount(123456)]);
echo "\n";
[1,2,3,4,6]
var_dump(log10(0));
var_dump(log10(-1));
var_dump(is_nan(log10(-1)));
float(-INF) float(NAN) bool(true)