Open In App

PHP pow( ) Function

Many times it happens that while solving mathematical expressions we require a number to be raised by a specific value. We also call this power of a number. Example of such cases is Exponential expression, cube root, square root, etc. Thus in such cases, PHP’s inbuilt function pow() comes to aid. The pow() function in PHP is used to calculate a base raised to the power of the exponent. It is a generic function that can be used with a number raised to any value. It takes two parameters which are the base and exponent and returns the desired answer. If both the arguments passed are non-negative integers and the result can be represented as an integer, the result is returned with integer type, otherwise, it is returned as a float. We already have discussed the pow() function in brief in the article PHP | Math functions. In this article, we will learn about the pow() function in detail. 

Syntax:



number pow($base, $exp)

Parameters: The pow() function accepts two parameters as shown in the above syntax:

Return Value: It returns a number (integer or floating-point) that is equal to $base raised to the power of $exponent



Examples:

Input : pow(3, 2)
Output : 9

Input : pow(-3, 2)
Output : 9

Input : pow(-3, -3)
Output : 0.037037037037037

Input : pow(-3, -3.2)
Output : NaN

The below programs illustrate the working of pow() in PHP:

When both the parameters passed are positive: 




<?php
  
echo(pow(3, 2));
  
?>

Output:

9

When the $base is negative and $exponent is a positive even number: 




<?php
  
echo(pow(-3, 2));
  
?>

Output:

9

When $base is negative and $exponent is a negative odd number: 




<?php
  
echo(pow(-3, -3));
  
?>

Output:

0.037037037037037

When $base is negative and $exponent is a negative number with decimal places: 




<?php
echo(pow(-3, -3.2));
?>

Output:

NaN

Important Points To Note:

Reference: http://php.net/manual/en/function.pow.php


Article Tags :