The pow()
function in PHP is used to calculate the power of a number, raising a base number to the exponent provided.
Syntax:
pow(base, exponent)
- base: The number to be raised to the power of.
- exponent: The power to which the base number will be raised.
Examples:
- Basic Usage:
<?php
echo pow(2, 3); // 2 raised to the power of 3 (2^3)
?>
Output:
8
Explanation: 2^3 = 2 * 2 * 2 = 8
- Using Float Values:
<?php
echo pow(5.5, 2); // 5.5 raised to the power of 2
?>
Output:
30.25
Explanation: 5.5^2 = 5.5 * 5.5 = 30.25
- Using Negative Exponent:
<?php
echo pow(2, -3); // 2 raised to the power of -3
?>
Output:
0.125
Explanation: 2^-3 = 1 / (2^3) = 1 / 8 = 0.125
- Raising a Negative Number:
<?php
echo pow(-2, 3); // -2 raised to the power of 3
?>
Output:
-8
Explanation: (-2)^3 = -2 * -2 * -2 = -8
- Raising Zero to Any Power:
<?php
echo pow(0, 5); // 0 raised to the power of 5
?>
Output:
0
Explanation: 0^5 = 0
Notes:
- The
pow()
function returns a float value.
- If the exponent is negative, it calculates the reciprocal of the base raised to the positive exponent.