Life of Pi - Working with Pi in PHP

Josh Sherman
1 min read
Software Development PHP

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;
Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Born again Linux user. Founder of Holiday API, Backend Engineering Manager and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Pears and Perils
Antifragile: Things That Gain from Disorder
Grinding It Out: The Making of McDonald's

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles