Mathematics7 min readExamples run on PHP 8.5.10

pi()

Learn how PHP 8.4 pi() returns an approximation of pi as a float, how it relates to M_PI, and how to format and compare results safely.

Signature

pi(): float

Purpose

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.

Example
<?php
echo number_format(pi(), 5), PHP_EOL;
var_dump(pi() === M_PI);
Output
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.

Example
<?php
$radius = 2.5;
$result = [
    'area' => round(pi() * $radius ** 2, 2),
    'circumference' => round(2 * pi() * $radius, 2),
];
echo json_encode($result), PHP_EOL;
Output
{"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.

Example
<?php
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;
Output
[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, while json_encode() and var_export() follow serialize_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

  1. Passing degrees to sin() or cos(). These expect radians. Convert first.
  2. Hard-coding 3.14. This loses accuracy needlessly. Use pi() or M_PI.
  3. Using == or === on computed floats. Prefer abs($a - $b) < $epsilon.
  4. Rounding too early. Round only the final value you display or store.
  5. Expecting echo pi() to be stable everywhere. Format explicitly with number_format() or round() when the exact text matters.
  6. Calling it with arguments. The function has none, so extra arguments are an error.
  • M_PI: the constant equivalent of pi().
  • deg2rad() and rad2deg(): convert between degrees and radians.
  • sin(), cos(), tan(): trigonometric functions that take radians.
  • round() and number_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

Display pi and compare it with M_PI
<?php
echo number_format(pi(), 5), PHP_EOL;
var_dump(pi() === M_PI);
Output
3.14159
bool(true)
Circle area and circumference
<?php
$radius = 2.5;
$result = [
    'area' => round(pi() * $radius ** 2, 2),
    'circumference' => round(2 * pi() * $radius, 2),
];
echo json_encode($result), PHP_EOL;
Output
{"area":19.63,"circumference":15.71}
Degrees to radians and float comparison
<?php
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;
Output
[3.1415926536,1.5707963268,false,true]