Signature
pi(): floatPurpose
pi() gives you the mathematical constant pi as a PHP float. You reach for it whenever a formula involves circles, spheres, angles or periodic waves: area, circumference, converting between degrees and radians, and so on. The manual notes that the constant M_PI yields results identical to pi(), so the two are interchangeable. The function form is handy when you prefer a call expression, for example when passing a callable name around. The constant is shorter inside long formulas.
Signature
Parameters and defaults
pi() takes no parameters, so there is nothing to configure and no default values. It always returns the same approximation on a given build.
Return value and failure cases
The return type is float. The value is an approximation, because a binary floating-point number cannot represent pi exactly. There is no failure return such as false or null. Because the function accepts zero arguments, passing any argument to a built-in function like this raises an ArgumentCountError, which is a programming mistake rather than a runtime condition you would handle.
Example 1: Display and compare with M_PI
The first example formats pi to five decimal places with number_format() and confirms that the function and the constant are identical.
echo number_format(pi(), 5), PHP_EOL;
var_dump(pi() === M_PI);3.14159 bool(true)
Formatting explicitly is better than relying on a bare echo pi(). When a float is converted to a string, PHP uses the precision ini setting, which is why the manual shows 3.1415926535898 for that call. An explicit format keeps your output independent of configuration.
Example 2: Circle measurements
A realistic use is geometry. This example computes the area and circumference of a circle with radius 2.5 and rounds both results to two decimals before encoding them as JSON.
$radius = 2.5;
$result = [
'area' => round(pi() * $radius ** 2, 2),
'circumference' => round(2 * pi() * $radius, 2),
];
echo json_encode($result), PHP_EOL;{"area":19.63,"circumference":15.71}
Rounding at the end, not in the middle, keeps rounding error from accumulating. Only the presentation value is rounded; the calculation uses the full float.
Example 3: Degrees, radians and float comparison
Trigonometric functions such as sin() and cos() work in radians. Converting degrees to radians multiplies by pi and divides by 180. The example also shows a classic float trap: sin(pi()) is not exactly zero.
function degreesToRadians(float $degrees): float
{
return $degrees * pi() / 180;
}
$values = [
round(degreesToRadians(180), 10),
round(degreesToRadians(90), 10),
sin(pi()) === 0.0,
abs(sin(pi())) < 1e-15,
];
echo json_encode($values), PHP_EOL;[3.1415926536,1.5707963268,false,true]
The third entry is false because the stored value is only close to true pi, so the sine comes out as a tiny nonzero number. The fourth entry uses a tolerance and is true. PHP also provides deg2rad(), which performs this conversion for you; writing it by hand is mainly useful for understanding the formula.
Edge cases
- Approximation: The result is a double-precision float. Roughly fifteen to seventeen significant decimal digits are meaningful, no more.
- Output depends on context: String conversion follows
precision, whilejson_encode()andvar_export()followserialize_precision. The same float may therefore print with different numbers of digits in different places. - Exact equality: Results of trigonometric formulas that involve pi rarely compare equal to ideal values. Compare with a tolerance.
- Integer contexts: Multiplying pi by an integer yields a float. If you need a whole number, round or cast deliberately and know which direction you are rounding.
Common mistakes
- Passing degrees to
sin()orcos(). These expect radians. Convert first. - Hard-coding
3.14. This loses accuracy needlessly. Usepi()orM_PI. - Using
==or===on computed floats. Preferabs($a - $b) < $epsilon. - Rounding too early. Round only the final value you display or store.
- Expecting
echo pi()to be stable everywhere. Format explicitly withnumber_format()orround()when the exact text matters. - Calling it with arguments. The function has none, so extra arguments are an error.
Related functions
M_PI: the constant equivalent ofpi().deg2rad()andrad2deg(): convert between degrees and radians.sin(),cos(),tan(): trigonometric functions that take radians.round()andnumber_format(): control how a float is rounded or shown.
Recap
pi() takes no arguments and returns pi as a float, identical to M_PI. It cannot fail at runtime, but the value is an approximation, so format output explicitly, round only at the end, and compare computed floats with a tolerance. Remember that trigonometric functions need radians.
Tested examples
echo number_format(pi(), 5), PHP_EOL;
var_dump(pi() === M_PI);
3.14159 bool(true)
$radius = 2.5;
$result = [
'area' => round(pi() * $radius ** 2, 2),
'circumference' => round(2 * pi() * $radius, 2),
];
echo json_encode($result), PHP_EOL;
{"area":19.63,"circumference":15.71}
function degreesToRadians(float $degrees): float
{
return $degrees * pi() / 180;
}
$values = [
round(degreesToRadians(180), 10),
round(degreesToRadians(90), 10),
sin(pi()) === 0.0,
abs(sin(pi())) < 1e-15,
];
echo json_encode($values), PHP_EOL;
[3.1415926536,1.5707963268,false,true]