Mathematics7 min readExamples run on PHP 8.5.10

log10()

Learn how PHP 8.4 log10() returns the base-10 logarithm of a number, how it behaves for zero and negatives, and where it helps in real code.

Signature

log10(float $num): float

Purpose

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

  • $num is 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) returns NAN.
  • log10(INF) returns INF.

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.

Example
<?php
var_dump(log10(1000));
var_dump(log10(1));
echo round(log10(2), 4), "\n";
Output
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.

Example
<?php
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";
Output
[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.

Example
<?php
var_dump(log10(0));
var_dump(log10(-1));
var_dump(is_nan(log10(-1)));
Output
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 int is widened to float, 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

  1. Assuming an int is returned. log10(100) is float(2), not int(2). Cast explicitly, or use intdiv-style logic elsewhere, if you need an integer.
  2. Passing zero or negative values without checking. You receive -INF or NAN, which can silently spread through later arithmetic.
  3. Using it for the natural logarithm. log10() is base 10 only. Use log() for base e, or log($num, 10) if you prefer one function with an explicit base.
  4. Trusting exact equality. Tests like log10($x) == 3 are fragile for computed $x.
  5. Forgetting to round for display. Printing raw floats can show long, noisy decimals.
  • log() – natural logarithm, or logarithm with a chosen base.
  • log1p() – accurate log(1 + x) for very small x.
  • exp() – the inverse of the natural logarithm.
  • pow() or the ** operator – raise 10 to a power, the inverse of log10().
  • floor(), round() – commonly paired with log10().
  • 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

Powers of ten and a fractional result
<?php
var_dump(log10(1000));
var_dump(log10(1));
echo round(log10(2), 4), "\n";
Output
float(3)
float(0)
0.301
Counting the digits of a positive integer
<?php
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";
Output
[1,2,3,4,6]
Zero and negative input
<?php
var_dump(log10(0));
var_dump(log10(-1));
var_dump(is_nan(log10(-1)));
Output
float(-INF)
float(NAN)
bool(true)