log10
Returns the base-10 logarithm of a number.
log10(float $num): float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | float | The value to calculate the base-10 logarithm for. |
Return value
floatThe base-10 logarithm of num.
log10() returns the base-10 logarithm: the power of ten that produces $num. It is equivalent to log($num, 10), just shorter and one level less indirect.
When to reach for it
Base 10 is the logarithm of human-facing scales. Use it for orders of magnitude — how many digits a number has, which SI bucket a byte count falls into, how many decades apart two values are. It is also the base built into decibels (10 * log10($ratio) for power ratios, 20 * log10($ratio) for amplitude), pH, the Richter scale, and any chart axis where you want tick marks at 1, 10, 100 and 1000.
Edge cases
log10(0) is -INF and a negative argument returns NAN. Neither raises a warning or throws, so a single zero slipping into a dataset silently contaminates every value computed from it. Guard the input before the call.
Exact powers of ten come back as exact integers — log10(1000) is precisely 3.0 — because the platform's log10() is correctly rounded for those. Values just below a boundary can still round upward, which is what makes digit counting via floor(log10($n)) + 1 fragile for very large integers.
Example
When to use it
- Picking a B/KB/MB/GB suffix by finding a byte count's order of magnitude.
- Converting a power or amplitude ratio into decibels.
- Positioning values on a logarithmic chart axis or generating log-spaced ticks.
- Deriving pH from a hydrogen ion concentration.
Frequently asked questions
What is the difference between log and log10 in PHP?
log() returns the natural logarithm, base e, unless you pass a second argument; log10() is permanently base 10. log10($n) and log($n, 10) produce identical values.
How do I count the digits of a number with log10?
Use (int) floor(log10($n)) + 1 for a positive integer. It is correct for everyday values, but floating-point rounding makes it unreliable near powers of ten for very large numbers, where strlen((string) $n) is safer.
What does log10 return for 0 or a negative number?
log10(0) returns -INF and a negative argument returns NAN. Neither raises an error, so validate the input before calling if a zero would be a bug.
More examples
Choosing a size suffix by order of magnitude
Dividing the base-10 logarithm by 3 groups values into thousands, so the floor of that gives an index straight into the suffix list. These are decimal units (1 KB = 1000 bytes); for binary units divide log(2) instead, or use log($bytes, 2) / 10.
<?php
$sizes = [512, 4096, 1048576, 3221225472];
foreach ($sizes as $bytes) {
$unit = (int) floor(log10($bytes) / 3);
$suffix = ['B', 'KB', 'MB', 'GB'][$unit];
printf("%-12d %6.2f %s\n", $bytes, $bytes / 10 ** ($unit * 3), $suffix);
}A log-scaled bar chart in the terminal
On a linear scale the 61000 row would be twenty thousand times longer than the first, which no terminal can show. Multiplying log10() by a constant compresses four orders of magnitude into a readable range, where each additional 8 characters means ten times more views.
<?php
$views = [3, 90, 2400, 61000];
foreach ($views as $v) {
$bar = str_repeat('#', (int) round(log10($v) * 8));
printf("%6d %s\n", $v, $bar);
}pH from concentration
pH is defined as the negative base-10 logarithm of hydrogen ion concentration, so the exponents come straight back out as 2.0, 7.0 and 13.0. Any scale defined as "the exponent of a power of ten" is a log10() call with a sign flip.
<?php
$concentrations = [
'lemon juice' => 1.0e-2,
'pure water' => 1.0e-7,
'bleach' => 1.0e-13,
];
foreach ($concentrations as $name => $h) {
printf("%-12s pH %.1f\n", $name, -log10($h));
}
Discussion