pow()
Today, we will discuss the pow() function in PHP. This function is used to raise a number to a power.
The pow() function raises a number to a power — that is, it computes base multiplied by itself exponent times. This page covers its syntax, the type of value it returns, how it differs from the ** operator, and the edge cases (negative, fractional, and zero exponents) that trip people up.
Syntax
pow(int|float $base, int|float $exponent): int|float|object$base— the number to be raised.$exponent— the power to raise the base to.
The function returns $base raised to the power of $exponent. The result type depends on the inputs: if both arguments are non-negative integers and the result fits in an integer, an int is returned; otherwise a float is returned.
Basic example
Here we raise 2 to the power of 3, store the result, and print it. Because both arguments are non-negative integers, pow() returns the integer 8.
pow() vs. the ** operator
Since PHP 5.6, the exponentiation operator ** does exactly the same thing as pow() and is usually preferred because it reads more naturally:
<?php
echo pow(2, 10); // 1024
echo "\n";
echo 2 ** 10; // 1024
?>Both produce 1024. The ** operator is right-associative, so 2 ** 3 ** 2 evaluates as 2 ** (3 ** 2), which is 512 — not 64. See PHP operators for more on operator precedence.
Negative, fractional, and zero exponents
pow() is not limited to whole positive exponents.
<?php
echo pow(2, -2); // 0.25 -> 1 / (2 * 2)
echo "\n";
echo pow(16, 0.5); // 4 -> the square root of 16
echo "\n";
echo pow(5, 0); // 1 -> any number to the power 0 is 1
echo "\n";
echo pow(-2, 3); // -8 -> a negative base with an integer exponent
?>Key behaviours:
- A negative exponent returns the reciprocal, so the result is a
float. - A fractional exponent computes a root: raising to
0.5is the square root,1/3the cube root. For square roots specifically,sqrt()is clearer. - Any base to the power 0 is
1. - A negative base raised to a non-integer exponent has no real result;
pow()returnsNAN(Not A Number) in that case.
Gotchas
- Large results overflow to float.
pow(2, 63)exceedsPHP_INT_MAXon a 64-bit system, so the result silently becomes a float and loses precision. Use it knowing the result type can change. NANfor impossible roots.pow(-8, 1/3)returnsNANrather than-2, because PHP evaluates it as a real-number power.- Strings are coerced.
pow("2", "3")works and returns8, but relying on string coercion is fragile — pass numbers.
Related functions
sqrt()— square root, equivalent topow($n, 0.5).abs()— absolute value, handy before taking a fractional power of a possibly-negative number.intdiv()— integer division.- PHP Math functions — overview of PHP's numeric toolkit.
Conclusion
pow() (or the equivalent ** operator) raises a number to a power and is a staple of any mathematical PHP code. Remember that the return type can shift from int to float depending on the arguments, that negative bases with fractional exponents yield NAN, and that very large results overflow to floats.