PHP pow() function


The pow() function in PHP is used to raise a number to a specified power (exponent). It takes two arguments: the base number and the exponent, and it calculates the result of raising the base to the power of the exponent.

Syntax:

pow(float $base, float $exponent): float
  • $base: The base number that you want to raise.
  • $exponent: The exponent to which the base number is raised.
  • Return Value: Returns the result as a float.

Example 1: Basic Exponentiation

<?php echo pow(2, 3); ?>

Output:

8

Explanation: 232^3 (2 raised to the power of 3) equals 8 because 2×2×2=82 \times 2 \times 2 = 8.

Example 2: Exponent with a Negative Base

<?php echo pow(-2, 4); ?>

Output:

16

Explanation: (2)4(-2)^4 equals 16 because 2×2×2×2=16-2 \times -2 \times -2 \times -2 = 16.

Example 3: Exponent with a Fractional Base

<?php echo pow(4, 0.5); ?>

Output:

2

Explanation: 40.54^{0.5} calculates the square root of 4, which is 2.

Example 4: Exponent with a Negative Exponent

<?php echo pow(2, -2); ?>

Output:

0.25

Explanation: 222^{-2} equals 0.25 because it calculates 122=14=0.25\frac{1}{2^2} = \frac{1}{4} = 0.25.

Example 5: Exponent of Zero

<?php echo pow(0, 5); ?>

Output:

0

Explanation: 050^5 equals 0 because any number raised to a positive power remains 0.

Example 6: Exponent with a Decimal Base

<?php echo pow(2.5, 3); ?>

Output:

15.625

Explanation: 2.532.5^3 equals 15.625 because 2.5×2.5×2.5=15.6252.5 \times 2.5 \times 2.5 = 15.625.

Key Points:

  • The pow() function can handle positive, negative, and fractional bases and exponents.
  • It returns the result as a float, even if the output is a whole number.
  • It is useful in various mathematical calculations, such as scientific computations, statistics, and geometry.

In summary, pow() is a versatile and powerful function in PHP for performing exponentiation, allowing developers to easily calculate powers of numbers.