The irrational mathematical constant of Pi can be obtained a few different ways in PHP. There is a function to give you the value as well as a bunch of constants that represent Pi as well as fractions of Pi and it’s square root. Heck, you could even do it the old fashioned way:

$pi = 245850922 / 78256779;

But that doesn’t make much sense when there’s so many built-ins. To obtain the value of Pi with PHP’s built in function is easy (and logically named ;):

$pi = pi();

But why use a function when you could simply reference a constant? I mean, Pi is a constant already, so for me this makes the most sense:

$pi = M_PI;

Then for fractions and square roots of pi we can utilize these:

$half_pi          = M_PI_2;
$quarter_pi       = M_PI_4
$one_over_pi      = M_1_PI;
$two_over_pi      = M_2_PI;
$sqrt_pi          = M_SQRTPI;
$two_over_sqrt_pi = M_2_SQRTPI;




Did you enjoy this post?

Cool if I slip into your inbox with more?
Full posts, 1-2 times per week: