Definition and Usage
The pow () function is used to compute power of a certain number. It returns xy calculation, also termed as x raised to y. PHP also provides "**" asexponentiation operator.
So, pow(x,y) returns xy which is same as x**y
Syntax
pow ( number $base , number $exp ) : number
Parameters
Sr.No | Parameter & Description |
---|---|
1 | base The base to be raised |
2 | exp power to which base needs to be raised |
Return Values
PHP pow() function returns base raised to power of exp. If both arguments are non-negative integers,the result is returned as integer, otherwise it is returned as a float.
PHP Version
This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.
Example
Following example calculates 102 using pow() function
<?php echo "pow(10,2) = " . pow(10,2); echo " using ** operator " . 10**2; ?>
Output
This will produce following result −
pow(10,2) = 100 using ** operator 100
Example
Any number raised to 0 results in 1. This is verified by following example −
<?php $x=10; $y=0; echo "pow(" . $x, "," . $y . ")=". pow($x,$y); ?>
Output
This will produce following result −
pow(10,0)=1
Example
Following example computes square root of 100 using pow() function;
<?php $x=100; $y=0.5; echo "pow(" . $x, "," . $y . ")=". pow($x,$y) . "\n"; echo "using sqrt() function : ". sqrt(100); ?>
Output
This will produce following result −
pow(100,0.5)=10 using sqrt() function : 10
Example
This example shows uses pow() function in calculation of area of circle.−
<?php $radius=5; echo "radius = " . $radius . " area = " . M_PI*pow(5,2); ?>
Output
This will produce following result −
radius = 5 area = 78.539816339745