To get the value of pi in php, the easiest way is use the php pi() function. pi() returns the value 3.1415926535898.

echo pi(); // 3.1415926535898

You can also use the php constant M_PI

echo M_PI; // 3.1415926535898

In php, we can easily get the value of pi for use in trigonometry with the php math functions.

For example, let’s say we want to find the sine of a number. The php sin() function takes a number in radians and then finds the sine of the number.

To find the sine of a number, we can use pi to make our lives easy.

To get the value of pi in your php code, you just call the pi() function as shown below.

echo pi(); // 3.1415926535898

Then, if you want to use it like in our example with finding the sine of a number, you can use the pi constant in the following way.

echo sin(pi()/3); // 0.8660254037844386

pi Related Constants You Can Use in php

In php, you can also use the following constants to get the value of pi without using the php pi() function.

These constants are faster than calling the pi() function.

echo M_PI;      // 3.14159265358979323846   pi
echo M_PI_2;    // 1.57079632679489661923    pi/2
echo M_PI_4;    // 0.78539816339744830962    pi/4
echo M_1_PI;    // 0.31830988618379067154    1/pi
echo M_2_PI;    // 0.63661977236758134308    2/pi
echo M_SQRTPI   // 1.77245385090551602729    sqrt(pi)
echo M_2_SQRTPI // 1.12837916709551257390    2/sqrt(pi)

Hopefully this article has been helpful for you to understand how to find the value of pi in php.

Categorized in:

PHP,

Last Update: March 20, 2024