PHP pi() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In PHP, pi() returns an approximate value of the mathematical constant pi (3.14159...).

Pi is the ratio of a circle's circumference to its diameter. You will use it for circles, spheres, angles, and trigonometry.

Syntax

Call pi() when you need the constant π. Start with this line.

PHP
$value = pi();
CallWhat it does
pi()Returns about 3.1415926535898.
round(pi(), 2)Returns 3.14.
2 * pi() * $radiusCalculates the circumference of a circle.

pi() takes no parameters and always returns the same value as the constant M_PI. Use whichever you find easier to read.

Library declarations
PHP
pi(): float

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
floatAn approximation of pi, the same value as M_PI.

PHP Examples

Example 1 — Print the Value of Pi

Call pi() with no arguments to get the value of pi.

PHP
<?php
echo pi(), PHP_EOL;
echo M_PI, PHP_EOL;

How It Works

  1. pi() takes no arguments, so the parentheses stay empty.
  2. PHP prints the float using its default display precision.
  3. M_PI is the built-in constant with the same value.
  4. Both lines print the same number.

Example 2 — Round and Compare Pi

Format pi for display and confirm that pi() equals M_PI.

PHP
<?php
echo round(pi(), 2), PHP_EOL;
echo number_format(pi(), 5), PHP_EOL;
echo pi() === M_PI ? "pi() equals M_PI" : "Different", PHP_EOL;

How It Works

  1. round(pi(), 2) keeps two decimal places and returns 3.14.
  2. number_format(pi(), 5) formats pi with five decimals.
  3. The strict comparison === checks that both values are identical.
  4. The ternary prints a message because they match.

Example 3 — Area and Circumference of a Circle

Use pi() in the classic circle formulas.

PHP
<?php
$radius = 5;
$area = pi() * $radius ** 2;
$circumference = 2 * pi() * $radius;

echo "Area: ", round($area, 2), PHP_EOL;
echo "Circumference: ", round($circumference, 2), PHP_EOL;

How It Works

  1. Set the radius to 5.
  2. Area uses the formula pi x r squared. The ** operator squares the radius.
  3. Circumference uses the formula 2 x pi x r.
  4. Round both values to two decimal places and print them.

Frequently Asked Questions

pi() returns a float that approximates the number pi, about 3.14159265358979. It takes no parameters.
There is no difference in value. M_PI is a constant and pi() is a function that returns the same number.
Use round(pi(), 2) to get 3.14, or number_format(pi(), 2) if you want a formatted string.
Use pi() * $radius ** 2. For example, a radius of 5 gives about 78.54.
No. Computers store pi as a floating-point number, so it is accurate to about 15 digits but not infinite.

Next: pow()

pow() raises a number to a power. For example, pow(2, 3) means 2 x 2 x 2, which is 8.

pow() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful