pi
Returns an approximation of pi as a float, identical to the M_PI constant.
pi(): float- Added in PHP 4.0
Return value
floatThe value of pi as a float, identical to the M_PI constant.
pi() returns an approximation of π as a float. It takes no arguments and is exactly identical to the M_PI constant — same bits, same value, and pi() === M_PI is true.
Which one should you use
Prefer M_PI. It is a constant, so it costs nothing at runtime and reads as a constant to anyone scanning the code, whereas pi() is a function call on every evaluation. pi() exists largely for historical reasons and for the rare place where a callable is more convenient than a constant. Neither is more precise than the other.
Precision versus display
echo pi() prints only around 14 digits, because the precision ini setting controls how floats are displayed, not how they are stored. The value itself is the closest double to π, good to roughly 16 significant digits. Use printf('%.15f', pi()) or var_dump() when you need to see them all.
Related constants save you the arithmetic: M_PI_2, M_PI_4, M_1_PI and M_2_PI. And remember that every trigonometric function in PHP works in radians, so degrees need deg2rad() or a * pi() / 180.
Example
When to use it
- Converting degrees to radians before calling sin(), cos() or tan().
- Computing the area, circumference or arc length of a circle.
- Generating evenly spaced points around a circle for an SVG or a chart.
- Normalising an angle returned by atan2() into a 0 to 360 degree range.
Frequently asked questions
What is the difference between pi() and M_PI in PHP?
Nothing in value: pi() === M_PI is true. M_PI is a constant and avoids a function call on every evaluation, so it is the usual choice.
Why does echo pi() only show 14 digits?
The precision ini setting controls how floats are printed, not how they are stored. The value holds about 16 significant digits; printf('%.15f', pi()) or var_dump() reveals them.
How do I convert degrees to radians for sin() and cos()?
Multiply by pi() / 180, or call deg2rad(), which does exactly that. Every trigonometric function in PHP takes radians, never degrees.
More examples
Points evenly spaced around a circle
A full turn is 2*pi radians, so dividing it into n steps places the points at equal intervals around the circle — the standard way to lay out a pie chart, a radial menu or a polygon. Point 0 lands exactly on (10.000, 0.000), because sin(0) is exactly zero. The floating-point residue shows up at point 3, half a turn round: sin(M_PI) is 1.2246e-16 rather than 0, so y is really 1.2246e-15 and only prints as 0.000 because pi cannot be represented exactly in binary.
<?php
$n = 6;
$radius = 10;
for ($i = 0; $i < $n; $i++) {
$angle = 2 * pi() * $i / $n;
printf("point %d: x=%7.3f y=%7.3f\n",
$i, cos($angle) * $radius, sin($angle) * $radius);
}Degrees to radians
The manual conversion and deg2rad() agree to every digit, since deg2rad() is exactly this multiplication. Watch the last row: sin(180 degrees) prints as 0.0000 but is not exactly zero — pi cannot be represented exactly in binary, so trigonometric identities come out to within rounding error rather than dead on.
<?php
foreach ([0, 30, 45, 90, 180] as $deg) {
$rad = $deg * pi() / 180;
printf("%3d deg = %.6f rad (deg2rad: %.6f) sin = %.4f\n",
$deg, $rad, deg2rad($deg), sin($rad));
}pi() and the related constants
pi(), M_PI and acos(-1) all produce the identical double. M_PI_2 and M_PI_4 are pi halved and quartered, handy for right angles, while M_2_PI is 2 divided by pi rather than 2 times pi — a naming trap worth knowing before you use it.
<?php
printf("pi() %.15f\n", pi());
printf("M_PI %.15f\n", M_PI);
printf("M_PI_2 %.15f\n", M_PI_2);
printf("M_PI_4 %.15f\n", M_PI_4);
printf("M_2_PI %.15f\n", M_2_PI);
printf("acos(-1) %.15f\n", acos(-1));
Discussion