PHP log10() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In PHP, log10() returns the base-10 logarithm of a number.

It tells you what power of 10 gives your number. For example, log10(1000) is 3 because 10 to the power 3 is 1000.

Syntax

Call log10() with the values you need. Start with this line.

PHP
$result = log10(1000);
CallWhat it does
log10(1000)Returns 3.
log10(100)Returns 2.
log10(0.01)Returns -2, because 10 to the power -2 is 0.01.

log10() is handy for counting digits, working with orders of magnitude, and scales like decibels or pH. For other bases use log() with a second argument.

Library declarations
PHP
log10(float $num): float

Parameters

ParameterDescription
numRequired. The number to take the base-10 logarithm of. It should be greater than 0.

Return Value

Return ValueDescription
floatThe base-10 logarithm of num. Returns -INF for 0 and NAN for negative numbers.

PHP Examples

Example 1 — Powers of Ten

Use log10() to find which power of 10 gives a number.

PHP
<?php
echo log10(1000), PHP_EOL;
echo log10(100), PHP_EOL;
echo log10(0.01), PHP_EOL;

How It Works

  1. log10(1000) asks: 10 to which power is 1000? The answer is 3.
  2. log10(100) returns 2 because 10 squared is 100.
  3. log10(0.01) returns -2 because 10 to the power -2 is 0.01.
  4. Results are floats, but whole values print without decimals.

Example 2 — Zero and Negative Input

See what log10() returns when the input is not a positive number.

PHP
<?php
echo log10(0), PHP_EOL;
echo log10(-5), PHP_EOL;
echo is_nan(log10(-5)) ? "Invalid input" : "Valid input", PHP_EOL;

How It Works

  1. log10(0) has no finite answer, so PHP returns -INF.
  2. log10(-5) is not a real number, so PHP returns NAN.
  3. is_nan() checks whether the result is NAN.
  4. The message tells the user the input was invalid.

Example 3 — Count the Digits in a Number

A neat trick: the number of digits in a positive integer is floor(log10(n)) + 1.

PHP
<?php
$number = 12345;
$digits = (int) floor(log10($number)) + 1;

echo "Digits: ", $digits, PHP_EOL;

How It Works

  1. Start with the number 12345.
  2. log10(12345) is about 4.09, which means the number is between 10^4 and 10^5.
  3. floor() drops the decimal part to get 4, then we add 1.
  4. The result 5 is the number of digits.

Frequently Asked Questions

log10() returns the base-10 logarithm of a number. It tells you which power of 10 gives that number.
Use (int) floor(log10($n)) + 1 for positive integers. It does not work for 0 or negative numbers, so handle those separately.
log() uses base e by default, while log10() always uses base 10. log10() is the clearer choice when you want powers of ten.
NAN means the input was negative. Logarithms of negative numbers are not real numbers. For 0 you get -INF instead.
PHP converts numeric strings such as "100" to a number automatically. A non-numeric string throws a TypeError in PHP 8.

Next: log1p()

log1p() returns the natural logarithm of 1 plus a number, written as log(1 + num).

log1p() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful