PHP log() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In PHP, log() returns the logarithm of a number. By default it gives the natural logarithm, which uses the base e.

A logarithm answers the question: "What power do I raise the base to, to get this number?" Pass a second argument if you want a different base, such as 2 or 10.

Syntax

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

PHP
$result = log(100, 10);
CallWhat it does
log(M_E)Returns 1, because e raised to the power 1 is e.
log(100, 10)Returns 2, because 10 raised to the power 2 is 100.
log(8, 2)Returns 3, because 2 raised to the power 3 is 8.

log($x) with no base is the natural logarithm. If you only need base 10 or base 2, log10() and log2-style calls like log($x, 2) are easy to read. A base of 0 or less throws a ValueError in PHP 8.

Library declarations
PHP
log(float $num, float $base = M_E): float

Parameters

ParameterDescription
numRequired. The number to take the logarithm of. It should be greater than 0.
baseOptional. The base of the logarithm. The default is M_E (about 2.71828), which gives the natural logarithm.

Return Value

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

PHP Examples

Example 1 — Natural Log and Custom Bases

Call log() with one argument for the natural logarithm, or add a base as the second argument.

PHP
<?php
echo log(M_E), PHP_EOL;
echo log(100, 10), PHP_EOL;
echo log(8, 2), PHP_EOL;

How It Works

  1. log(M_E) uses the default base e, so the answer is 1.
  2. log(100, 10) asks: 10 to which power gives 100? The answer is 2.
  3. log(8, 2) asks: 2 to which power gives 8? The answer is 3.
  4. Each result is a float, but PHP prints whole numbers without a decimal point.

Example 2 — Zero and Negative Numbers

Logarithms are only defined for positive numbers. See what PHP returns for the edge cases.

PHP
<?php
echo log(0), PHP_EOL;
echo log(-1), PHP_EOL;
echo is_nan(log(-1)) ? "Not a number" : "A number", PHP_EOL;

How It Works

  1. log(0) goes toward negative infinity, so PHP returns -INF.
  2. log(-1) has no real answer, so PHP returns NAN.
  3. Use is_nan() to test whether a result is NAN.
  4. The ternary prints a friendly message instead of the raw NAN value.

Example 3 — Years to Double Your Money

A common real-world use of logarithms is finding how long growth takes.

PHP
<?php
$rate = 0.07;
$years = log(2) / log(1 + $rate);

echo "Years to double: ", round($years, 2), PHP_EOL;

How It Works

  1. Set a growth rate of 7 percent as 0.07.
  2. Doubling means growing by a factor of 2, so we use log(2).
  3. Divide by log(1 + $rate) to get the number of years.
  4. Round to two decimals and print the result.

Frequently Asked Questions

log() returns the logarithm of a number. With one argument it returns the natural logarithm (base e). With a second argument it uses that base instead.
Pass 2 as the second argument, for example log(8, 2), which returns 3. This works for any base greater than 0 except 1.
log() uses base e by default, while log10() always uses base 10. log($x, 10) gives the same result as log10($x), but log10() is shorter and clearer.
Logarithms are not defined for negative numbers, so PHP returns NAN. For 0 the result goes toward negative infinity, so PHP returns -INF.
Yes. PHP does not have a function called ln(). The one-argument form of log() is the natural logarithm, which mathematicians write as ln.

Next: log10()

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

log10() 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