PHP Program to Find Power of Number
In PHP, you can find the power of a number using the built-in function
pow(base, exponent) or by using a loop to multiply the base number repeatedly.Examples
Inputbase=2, exponent=3
Output8
Inputbase=5, exponent=0
Output1
Inputbase=3, exponent=4
Output81
How to Think About It
To find the power of a number, you multiply the base number by itself as many times as the exponent says. For example, 2 to the power 3 means 2 × 2 × 2. You can do this by using PHP's built-in
pow function or by writing a loop that multiplies the base repeatedly.Algorithm
1
Get the base number and exponent from the user or input.2
If using a loop, start with a result of 1.3
Multiply the result by the base number repeatedly, as many times as the exponent.4
Return or print the final result.Code
php
<?php $base = 2; $exponent = 3; $result = pow($base, $exponent); echo "Result: $result"; ?>
Output
Result: 8
Dry Run
Let's trace the example where base=2 and exponent=3 using the pow function.
1
Set base and exponent
base = 2, exponent = 3
2
Calculate power
result = pow(2, 3) which means 2 × 2 × 2
3
Output result
Print 'Result: 8'
| Step | Operation | Value |
|---|---|---|
| 1 | Set base | 2 |
| 2 | Set exponent | 3 |
| 3 | Calculate pow(2,3) | 8 |
| 4 | Print result | Result: 8 |
Why This Works
Step 1: Using pow function
The pow(base, exponent) function calculates the base raised to the power of the exponent directly.
Step 2: Multiplying repeatedly
If you use a loop, you multiply the base by itself exponent times to get the same result.
Step 3: Outputting the result
The final number is printed using echo to show the power calculation.
Alternative Approaches
Using a for loop
php
<?php $base = 2; $exponent = 3; $result = 1; for ($i = 0; $i < $exponent; $i++) { $result *= $base; } echo "Result: $result"; ?>
This method manually multiplies the base repeatedly and is useful if you want to understand the process without built-in functions.
Using recursion
php
<?php function power($base, $exponent) { if ($exponent == 0) return 1; return $base * power($base, $exponent - 1); } $result = power(2, 3); echo "Result: $result"; ?>
This method uses a function that calls itself to calculate power, which is elegant but can be less efficient for large exponents.
Complexity: O(n) time, O(1) space
Time Complexity
Using the built-in pow() function is optimized internally, but a loop multiplies the base n times, so it is O(n).
Space Complexity
The program uses a fixed amount of memory regardless of input size, so space complexity is O(1).
Which Approach is Fastest?
The built-in pow() function is fastest and recommended; loops and recursion are slower but good for learning.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Built-in pow() | O(1) or optimized | O(1) | Fastest and simplest |
| For loop | O(n) | O(1) | Understanding multiplication process |
| Recursion | O(n) | O(n) | Elegant code but uses more memory |
Use PHP's built-in
pow() function for simple and fast power calculations.Forgetting that any number to the power of 0 is 1, so always handle exponent 0 correctly.