pow

Raises a base number to the power of an exponent and returns the result.

Signature
pow(mixed $num, mixed $exponent): int|float|object
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$nummixedThe base to use. Normally an int or float, but extension objects such as GMP numbers are accepted.
$exponentmixedThe exponent. May be negative or fractional, in which case a float is returned.

Return value

int|float|objectnum raised to the power of exponent. Returns int when both arguments are non-negative integers and the result fits in an int; otherwise returns float. Extensions such as GMP may return an object.

Errors

Throws a TypeError when an argument is not a number and cannot be converted to one. As of PHP 8.4, pow(0, negative) emits a deprecation notice.

Changelog

  • PHP 8.4.0Raising 0 to a negative exponent is deprecated. It still returns INF, but emits a deprecation notice.

pow() is the function form of the ** operator. pow(2, 10) and 2 ** 10 compute the same thing, but the function version can be passed around as a callable, which is why it still shows up in array_map() and array_reduce() calls.

Integer or float?

The return type is not fixed. If both arguments are non-negative integers and the answer fits inside PHP_INT_MAX, you get an int. Everything else — a negative exponent, a fractional exponent, a float argument, or an overflowing result — gives you a float. That means pow(2, 62) is an int but pow(2, 64) silently becomes an imprecise float. Check with is_int() if the distinction matters.

Things that return NAN

A negative base with a non-integer exponent has no real result, so pow(-8, 1/3) is NAN, not -2. Test with is_nan(); NAN == NAN is false, so a plain comparison will not catch it.

Precedence trap

The operator form binds tighter than unary minus: -2 ** 2 is -4. Write (-2) ** 2 or pow(-2, 2) when you want 4.

For money or arbitrary precision work, use bcpow() instead — floats will drift.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Compute compound interest or exponential growth over n periods
  • Convert byte counts between units by raising 1024 to a power
  • Apply a gamma or easing curve to a normalised 0-1 animation value
  • Score search results with an exponential decay factor

Frequently asked questions

What is the difference between pow() and the ** operator?

There is no behavioural difference in the value returned - pow(2, 10) and 2 ** 10 are identical. Use the function when you need to pass it as a callable or keep older-style code consistent, and watch that -2 ** 2 is -4 because unary minus binds looser than **.

Why does pow() return NAN?

Because you raised a negative base to a non-integer exponent, which has no real-number answer. pow(-8, 1/3) returns NAN rather than -2; test for it with is_nan(), since NAN fails every comparison.

Does pow() return an int or a float?

It returns an int only when both arguments are non-negative integers and the result fits within PHP_INT_MAX. A float argument, a negative or fractional exponent, or an overflowing result all produce a float.

More examples

pow() versus the ** operator

The operator and the function produce identical values, but the operator has a precedence surprise: -2 ** 2 parses as -(2 ** 2). Wrapping the base in parentheses, or using pow(), removes the ambiguity.

Example · php
<?php
var_dump(pow(3, 4) === 3 ** 4);  // bool(true)

// Precedence: unary minus binds looser than **
echo -2 ** 2, "\n";      // -4
echo (-2) ** 2, "\n";     // 4
echo pow(-2, 2), "\n";    // 4

Using pow() as a callable

The first block squares every element of an array. The second prints 1, 1024, 1048576 and 1073741824 - all still integers, because the base and exponent are non-negative ints and the results fit in PHP_INT_MAX.

Example · php
<?php
$bases = [2, 3, 4, 5];
$squares = array_map(fn($n) => pow($n, 2), $bases);
print_r($squares);  // [4, 9, 16, 25]

// Byte units built from powers of 1024
foreach (['B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3] as $unit => $p) {
    echo $unit, ' = ', pow(1024, $p), "\n";
}

Detecting NAN and integer overflow

Shows the two silent failure modes. A negative base with a fractional exponent gives NAN, which fails every comparison including against itself. And once the result exceeds PHP_INT_MAX the return type flips to float, so exactness is gone.

Example · php
<?php
$r = pow(-4, 0.5);
var_dump(is_nan($r));      // bool(true)
var_dump($r == $r);        // bool(false) - NAN never equals itself

var_dump(is_int(pow(2, 62)));  // bool(true)
var_dump(is_int(pow(2, 64)));  // bool(false) - overflowed to float
echo pow(2, 64), "\n";         // 1.844674407371E+19

Discussion

  • Be the first to comment on this lesson.