Open In App

PHP pow( ) Function

Last Updated : 22 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • $base: It is used to specify the base.
  • $exponent: It is used to specify the exponent.

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




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


Output:

9

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

PHP




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


Output:

9

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

PHP




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


Output:

0.037037037037037

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

html




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


Output:

NaN

Important Points To Note:

  • pow() function is used to solve exponential expressions.
  • It is a generic function that can be used for any exponent value.
  • Avoid using the pow() function if you want to calculate the square root of a function since PHP already has an inbuilt function for calculating square roots and it is much more efficient than pow().

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



Similar Reads

PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)
11. is_nan() : This function takes any value as argument and checks whether a value is number or not. It returns TRUE (1) if the specified value is 'not a number', otherwise it returns FALSE/NOTHING. Syntax : is_nan(value); Example : C/C++ Code &amp;lt;?php echo is_nan(234) . &amp;quot;\n&amp;quot;; echo is_nan(asin(1.1)) ; ?&amp;gt; Output : 1 Not
4 min read
PHP Program to Calculate pow(x, n)
Calculating the power of a number is a common mathematical operation. In PHP, this can be done using the pow( ) function, which takes two arguments, the base x and the exponent n, and returns x raised to the power of n. In this article, we will explore different approaches to calculate pow(x, n) in PHP, including using the built-in function and imp
3 min read
p5.js pow() function
The pow() function in p5.js is used to calculate the power of a number to the given number. It is an efficient way of multiplying numbers by themselves (or their reciprocals) in large quantities. Syntax pow( n, e ) Parameters: The function accepts two parameters as mentioned above and described below: n: This parameter stores the base of the expone
1 min read
D3.js pow.tickFormat() Function
The pow.tickFormat() function is used to change the format of the tick values. It returns a number format function that is suitable for displaying a tick value. Syntax: pow.tickFormat( count, specifier ) Parameters: This function accepts two parameters as mentioned above and described below. count: It is the number of tick values to be used. It is
2 min read
D3.js pow.exponent() Function
The pow.exponent() function is used to specify the exponent to the given value. It returns the current exponent when the exponent is not specified. The value of the current exponent defaults to one. Syntax: pow.exponent( exponent ) Parameters: This function accepts one parameter as mentioned above and described below. exponent: This parameter accep
2 min read
D3.js pow.invert() Function
The pow.invert() function is used to return a value from the domain when given a value from the range. This inversion is useful for interaction such as determining the data value that corresponds to the position of the mouse. Syntax: pow.invert( value ) Parameters: This function accepts only one parameter as given above and described below: value:
2 min read
D3.js pow.clamp() Function
The pow.clamp() function in d3.js is used to enable the clamp or disable the clamp only if the clamp is specified. The range of the return value may be outside the given range if the clamp is disabled. Syntax: pow.clamp(clamp); Property Values: This function accepts single parameter as given above and described below. clamp: It accepts a boolean va
2 min read
D3.js pow.rangeRound() Function
The pow.rangeRound() function is used set the range of the scale to the specified array of values along with this it internally sets the interpolator to interpolatorRound. Syntax: pow.rangeRound([range]); Parameters: This function takes a single parameter that is given above and described below. [range]: This is an array that contains the range for
2 min read
D3.js pow.ticks() Function
The pow.ticks() function is used to return the count values from the scale’s domain. The values returned by ticks() lies in the domain. If the count is not given as a parameter then by default it is set to 10. Syntax: pow.ticks([count]); Parameters: This function accepts a single parameter as mentioned above and described below. count: It is the nu
2 min read
D3.js pow.domain() Function
The pow.domain() function is used to set the scale’s domain to the specified array of numbers. The array specified here must contain two or more than two elements. Syntax: pow.domain([domain]); Parameters: This function takes a single parameter that is given above and described below. [domain]: An array that takes two or more values that specify th
1 min read