PHP pow()

pow(x,y) returns x power of y. x, y can be integers or floats, or negative numbers. Similarly, an integer or float will be returned depending on the parameters and result.

<?PHP
	echo pow(2,4);  //16
	
	echo pow(-2,3);  //-8
	echo pow(8,-3);	 //0.001953125, =1/pow(8,3)	
?>

Example of float parameters.

<?PHP
	echo pow(3.4,2.1);  //13.064889045095
?>

For nth root of number x (nx):

<?PHP
	echo pow(3.4,1/5);  //5th root of 3.4, =1.277308
	echo pow(8,1/3);  //cube root of 8, =2	
?>

** operator can be used as the Power operation in PHP 5.6 and higher.

<?PHP
	echo 2**3; //8
?>

The following customized function powx() can calculate an numeric array raised to the power of a number.

<?PHP
    function powx(&$arr, $x)
	{
		for ($ii=0;$ii<count($arr);$ii++)
			$arr[$ii] = pow($x,$arr[$ii]);
	}
    $arr2 = array("2","3.4","5.342","0","3","-5","6.3");
	powx($arr2,2);	
    print_r($arr2);
	
	//the result is:
	Array
	(
		[0] => 4
		[1] => 10.556063286183
		[2] => 40.560400705102
		[3] => 1
		[4] => 8
		[5] => 0.03125
		[6] => 78.793242454075
	)	
?>



:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • str_replace • strcmp
 • strpos • strrev
 • strrpos • strtr
 • substr • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expression • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2020  Terms of Use | Home