Raise a Number to a Power in PHP

You might need to raise a number to the power of another (the exponent) in a PHP script, for example \(2^3=2×2×2\) (sometimes written \(2^3\)), or in general \(x^n\). This may also be used with negative powers where \(x^{−n}=\frac{1}{x^n}\). Of course, you’d probably want to do this for more complicated examples!

There are two ways to do this in PHP – the exponent operator (**) and the pow() function. We’ll show you each in turn below.

Using the Exponent Operator in PHP

The exponent operator in PHP is represented by two asterisks (**). It’s used to raise a number to a power, which is the product of multiplying a number by itself a certain number of times. For example, 2 to the power of 3 is 2 x 2 x 2, which equals 8.

Here’s the basic syntax of the exponent operator in PHP:

$result = $base ** $exponent;

In this example, $base is the number you want to raise to a power, and $exponent is the power you want to raise it to. $result is the result of the exponent operation.

Let’s take a look at some simple examples of using the exponent operator in PHP:

// Calculate 2 to the power of 3
$result = 2 ** 3; // Output: 8

// Calculate 4 to the power of 2
$result = 4 ** 2; // Output: 16

// Calculate 10 to the power of -2
$result = 10 ** -2; // Output: 0.01

As you can see from the examples, the exponent operator can be used with both positive and negative powers.

Using the pow() function in PHP

In addition to using the exponent operator, you can also use the pow() function in PHP to raise a number to a power. The pow() function takes two arguments: the base number and the exponent. Here’s an example:

// Calculate 2 to the power of 3 using the pow() function
$result = pow(2, 3); // Output: 8

The pow() function is useful when you need to calculate a power using variables or when you want to store the result of the exponent operation in a variable.

Examples of ‘Power Of’ using the pow() function

Here are some examples, which raise a number to the power of another and display the output. In the examples, <br /> inserts an HTML line break to display the results neatly.

<?php
echo "3^5 = " . pow(3,5) . "<br />"; // 243
echo "2^9 = " . pow(2,9) . "<br />"; // 512
// Anything raised to the power of 0 is 1:
echo "99^0 = " . pow(99,0) . "<br />"; // 1
// Negative powers:
echo "3^-1 = 1/3 = " . pow(3,-1) . "<br />"; // 1/3 = 0.3333
echo "3^-2 = 1/(3^2) = " . pow(3,-2) . "<br />"; // 1/9 = 0.1111
echo "<br />";

// Loop through 2 raised to the 0,1,2,...,10:
for($i=0;$i<=10;$i++)
{
   echo "2^" . $i . " = " . pow(2,$i) . "<br />";
}
?>

And here is the output of this example code:

3^5 = 243
2^9 = 512
99^0 = 1
3^-1 = 1/3 = 0.33333333333333
3^-2 = 1/(3^2) = 0.11111111111111

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
2^10 = 1024

Below, we’ve outlined a couple of example cases where you might use these in practice.

Example: Calculating Compound Interest

The formula for calculating compound interest involves raising the interest rate to the power of the number of compounding periods. For example, to calculate the future value of an investment with a principal of $1,000, an interest rate of 5% compounded annually for 10 years, you would use the following formula in PHP:

$principal = 1000;
$interest_rate = 0.05;
$years = 10;

$future_value = $principal * (1 + $interest_rate) ** $years;

The code in this example performs the following steps to calculate this:

  1. It sets the principal amount to $1000 and assigns it to the $principal variable.
  2. It sets the interest rate to 5% and assigns it to the $interest_rate variable.
  3. It sets the number of years to 10 and assigns it to the $years variable.
  4. It uses the exponent operator ** to calculate the future value of the investment and assigns the result to the $future_value variable.

The future value is calculated using the formula \(FV = PV * (1 + r) ^ n\), where FV is the future value, PV is the present value (or principal), r is the interest rate, and n is the number of years.

The output of the code would be:

$future_value = 1628.89

This means that the future value of the investment after 10 years would be $1628.89, assuming the interest is compounded annually.

Example: Calculating Probabilities

The exponent operator can be used to calculate the probability of an event occurring multiple times in a row. For example, to calculate the probability of flipping a coin and getting heads three times in a row, you would use the following code in PHP:

$coin_prob = 0.5;
$num_flips = 3;

$prob_heads = pow($coin_prob, $num_flips);

This code performs the calculation as follows:

  1. It the probability of flipping heads on a single coin flip to 0.5 and assigns it to the $coin_prob variable.
  2. Then sets the number of coin flips to 3 and assigns it to the $num_flips variable.
  3. We use the pow() function to calculate the probability of getting heads on all three coin flips and assign the result to the $prob_heads variable.

The pow() function is used to raise the probability of flipping heads to the power of the number of coin flips. The result of this calculation is the probability of getting heads on all three coin flips.

The output of the code would be:

$prob_heads = 0.125

This means that the probability of flipping a coin and getting heads three times in a row is 0.125, or 12.5%.

Conclusion

In this tutorial you have seen how to raise a number to the power of another in PHP using two methods: the exponent operator and the pow() function.

You can also calculate square (and other) roots in PHP using the pow() function, which we’ve covered in a different article: Calculate Square Roots in PHP.