PHP pi() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

The pi () function returns the value of mathematical constant Π. It returns a float value 3.14159265359 which is equal to predefined constant defined in PHP - M_PI

Syntax

pi ( void ) : float

Parameters

This function requires no parameters

Return Values

PHP pi() function returnsthe mathematical constant Π and is equal to predefined mathematical constant M-PI. Instead of using M_PI, we can use pi() function in mathematical expressions.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following exampleuses pi() function in calculation of area of circle.

<?php
   $radius = 5;
   echo "area of circle with radius = " . $radius . " is " . pi()*pow($radius,2);
?>

Output

This will produce following result −

area of circle with radius = 5is 78.539816339745

Example

 Live Demo

Following example uses pi() function to calculate Π/2 and Π/4. For these angles PHP has M_PI_2 and M_PI-4 predefined constants respectively .−

<?php
   echo "pi/2 = " . pi()/2;
   echo " M-PI_2 = " . M_PI_2 . "\n";
   echo "pi/4 = " . pi()/4;
   echo " M_PI_4 = ". M_PI_4;
?>

Output

This will produce following result −

pi/2 = 1.5707963267949 M-PI_2 = 1.5707963267949
pi/4 = 0.78539816339745 M_PI_4 = 0.78539816339745

Example

 Live Demo

Using pi() function, value of 1/Π and 2/Π is calculated. These values are equivalent to M_1_PI and M_2_PI respectively;

<?php
   echo "1/pi = " . 1/pi();
   echo " M_1_PI = " . M_1_PI . "\n";
   echo "2/pi = " . 2/pi();
   echo " M_2_PI = ". M_2_PI;
?>

Output

This will produce following result −

1/pi = 0.31830988618379 M_1_PI = 0.31830988618379
2/pi = 0.63661977236758 M_2_PI = 0.63661977236758

Example

 Live Demo

This example shows sqrt(Π) and 2/sqrt(Π) calculated using pi() function are similar to M_SQRTPI and M_2_SQRTPI.−

<?php
   echo "sqrt(pi) = " . sqrt(pi());
   echo " M_SQRTPI = " . M_SQRTPI . "\n";
   echo "2/sqrt(pi) = " . 2/sqrt(pi());
   echo " M_2_SQRTPI = ". M_2_SQRTPI;
?>

Output

This will produce following result −

sqrt(pi) = 1.7724538509055 M_SQRTPI = 1.7724538509055
2/sqrt(pi) = 1.1283791670955 M_2_SQRTPI = 1.1283791670955
raja
Published on 30-Jun-2020 10:44:02
Advertisements