Open In App

PHP pi( ) Function

While solving mathematical problems we often come across questions which requires the value of π. Manually inserting the value of PI (π) can be time-consuming and erroneous. It is also not considered as a good programming practice. To solve this issue a built-in function of PHP pi() comes to aid.

The pi() function in PHP is used to return the value of π . Also, M_PI is a named constant PHP which gives identical value to that of returned by pi() function. It is slightly faster than the pi() function.



Some other predefined named constants related to π are :

Syntax:



float pi()

Parameters: This function does not accept any parameter.

Return Value: It returns a floating point value which is an approximate value of PI.

Examples:

Input : echo(pi())
Output : 3.1415926535898

Input : echo M_PI
Output : 3.1415926535898

Below programs illustrate the pi() function in PHP:

  1. When pi() function is used:




    <?php
      
    echo(pi());
      
    ?>
    
    

    Output:

    3.1415926535898
  2. When M_PI is used for finding the value of PI:




    <?php
      
    echo M_PI;
      
    ?>
    
    

    Output:

    3.1415926535898

Important Points To Note:

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

Article Tags :