pi() function in PHP

The pi() function returns the value of Pi (π), which is approximately 3.1415926535898. This mathematical constant represents the ratio of a circle's circumference to its diameter.

Syntax

pi()

Parameters

The pi() function takes no parameters.

Return Value

Returns the approximate value of PI as a floating-point number: 3.1415926535898.

Example

Here's a basic example using the pi() function −

<?php
    echo pi();
?>
3.1415926535898

Using M_PI Constant

PHP also provides the M_PI predefined constant as an alternative to the pi() function −

<?php
    echo M_PI;
?>
3.1415926535898

Practical Example

Calculate the area of a circle using the pi() function −

<?php
    $radius = 5;
    $area = pi() * pow($radius, 2);
    echo "Area of circle with radius $radius is: " . $area;
?>
Area of circle with radius 5 is: 78.539816339745

Conclusion

The pi() function provides easy access to the mathematical constant Pi in PHP. Both pi() and M_PI return the same value and can be used interchangeably in calculations.

Updated on: 2026-03-15T07:29:37+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements