PHP pi() function


Here’s a detailed explanation of the pi() function in PHP with an example and output.

What is pi()?

The pi() function in PHP returns the value of the mathematical constant π (pi), which is approximately 3.1415926535898.

Syntax:

pi()

Example with Output:

<?php echo pi(); // Outputs: 3.1415926535898

Output:

3.1415926535898

Example with a Practical Use Case:

Let’s calculate the circumference and area of a circle using pi().

Example Code:

<?php $radius = 7; // Calculate circumference $circumference = 2 * pi() * $radius; echo "Circumference of the circle: " . $circumference . "\n"; // Calculate area $area = pi() * pow($radius, 2); echo "Area of the circle: " . $area; ?>

Output:

Circumference of the circle: 43.982297150257 Area of the circle: 153.9380400259

In this example:

  • The circumference of the circle is calculated using the formula 2πr2 \pi r.
  • The area is calculated using the formula πr2\pi r^2, where r is the radius.

The pi() function is essential for performing any mathematical operations involving circles in PHP.