W3docs

exp()

Today, we will discuss the exp() function in PHP. This function is used to calculate the exponential value of a number.

The exp() function in PHP returns e raised to the power of a given number — that is, e^x. Here e (Euler's number, roughly 2.71828) is the base of the natural logarithm and appears throughout science and finance: in compound interest, population growth, radioactive decay, and probability. This page covers the syntax, the return value, several worked examples, the relationship to its inverse log(), and the common mistakes to avoid.

Syntax

exp(float $num): float
  • $num — the exponent. Any number (positive, negative, zero, or a float).
  • Return value — a float: the value of e raised to $num.

Because e is irrational, the result is almost always a non-terminating decimal. PHP prints it using the precision set by the precision php.ini directive (14 significant digits by default).

Basic example

php— editable, runs on the server

We pass 2 to exp(), so it computes . The result, 7.3890560989307, is 2.71828... multiplied by itself.

Key reference values

A few inputs are worth memorizing because they show what the function is doing:

<?php
echo exp(0), "\n";  // 1            (anything^0 is 1)
echo exp(1), "\n";  // 2.718281828459   (this IS e, same as the M_E constant)
echo exp(-1), "\n"; // 0.36787944117144 (e^-1 = 1/e)
echo exp(0.5), "\n";// 1.6487212707001  (the square root of e)
?>
  • exp(0) is 1, since any base raised to the power 0 equals 1.
  • exp(1) equals PHP's built-in constant M_E. You can use M_E directly when you just need the value of e and don't need to recompute it.
  • A negative exponent gives a value between 0 and 1 (e^-x = 1/e^x), which is how exponential decay is modeled.

exp() is the inverse of log()

The natural logarithm, PHP's log() function, undoes exp() and vice versa. Composing them returns the original number:

<?php
$x = 5;

echo log(exp($x)), "\n"; // 5
echo exp(log($x)), "\n"; // 5
?>

This relationship is the reason exp() shows up whenever you have a model expressed with natural logs and need to convert back to a normal scale.

Practical example: continuous compound interest

A classic real-world use of exp() is the continuous compound interest formula, A = P · e^(r·t):

<?php
$principal = 1000;  // starting amount
$rate      = 0.05;  // 5% annual rate
$years     = 10;

$amount = $principal * exp($rate * $years);

echo round($amount, 2); // 1648.72
?>

After 10 years, $1000 compounded continuously at 5% grows to about $1648.72.

exp() vs. pow()

It is easy to confuse exp() with pow():

  • exp($x) always uses e as the base — it is pow(M_E, $x).
  • pow($base, $exp) lets you choose any base, e.g. pow(2, 10) for 2¹⁰.

So exp(3) and pow(M_E, 3) produce the same result, but reach for pow() whenever the base is something other than e.

Common gotchas

  • Don't pass a string-with-units. exp("2 apples") is coerced to exp(2) (with a warning in modern PHP). Always pass a clean numeric value.
  • Large inputs overflow to INF. exp(710) exceeds the range of a PHP float and returns INF. Guard against very large exponents if the input is user-supplied.
  • Display precision is not loss of precision. The full float is stored internally; only the echoed string is truncated. Use printf() or number_format() to control how many digits you show.

Conclusion

exp() returns e^x and is the building block for any natural-exponential math in PHP — growth, decay, and continuous compounding. Remember that it is the inverse of log(), that exp(1) equals the M_E constant, and that you should use pow() when you need a base other than e. For the inverse-square-root style operations see also sqrt().

Practice

Practice
What does the PHP function exp() do?
What does the PHP function exp() do?
Was this page helpful?