PHP | sqrt( ) Function

Many a times it happens that while solving mathematical expressions we require to calculate the square root of a number.
To solve this issue like other programming language PHP provides us with a built-in function sqrt() which can be used to calulate the square root of a number. The sqrt() function in PHP is used to calculate the square root of a number.
We already have discussed about sqrt() function in brief in the article PHP | Math functions. In this article we will learn about the pow() function in details.

Syntax:

float sqrt(value)

Parameters : This function accepts a single parameter $value. It is the number whose square root you want to know.



Return Value: It returns a floating point value which is the square root of the argument $value passed to it.

Examples:

Input : sqrt(25) 
Output : 5

Input : sqrt(-25) 
Output : NaN

Input : sqrt(0.09) 
Output :0.3

Input : sqrt(0)
Output : 0

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

  1. When a positive number is passed as a parameter:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    echo(sqrt(25));
      
    ?>

    chevron_right

    
    

    Output:

    5
  2. When a negative number is passed as a parameter:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    echo(sqrt(-25));
      
    ?>

    chevron_right

    
    

    Output:

    NaN
  3. When a number with decimal places is passed as a parameter:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    echo(sqrt(0.09));
      
    ?>

    chevron_right

    
    

    Output:

    0.3
    
  4. When zero is passed as a parameter:
    filter_none
    edit close
    play_arrow
    link brightness_4 code
    <?php
      
    echo(sqrt(0));
      
    ?>
    chevron_right
    
    

    Output:

    0

Important Points To Note:

  • sqrt() function is used to calculate the square root of a number.
  • It is much more efficient than the generic method of square root calculation.
  • Precision depends on the user's precision directive.

Reference:
http://php.net/manual/en/function.sqrt.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.