PHP | pow( ) Function

Many a 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 an aid.
The pow() function in PHP is used to calculate a base raised to the power of exponent. It is a generic function which can be used with 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 about pow() function in brief in the article PHP | Math functions. In this article we will learn about the pow() function in details.

Syntax:

number pow($base, $exp)

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

  1. $base : It is used to specify the base.
  2. $exponent : It is used to specify the exponent.

Return Value: It returns a number (integer or floating-point) which 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

Below programs illustrate the working of pow() in PHP:

  1. When both the parameters passed are positive:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    9
  2. When the $base is negative and $exponent is a positive even number:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    9
  3. When $base is negative and $exponent is a negative odd number:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    0.037037037037037
  4. When $base is negative and $exponent is a negative number with decimal places:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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 pow() function if you want to calculate 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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.