How to use Functions and Constants for Pi In PHP
The area of a circle is calculated using the formula π * r2, where r is the radius of the circle and π is a mathematical constant approximately equal to 3.14159. The perimeter (circumference) of a circle is calculated using the formula 2 * π * r. we will create a function and pass the value of radius and return the area and perimeter.
Example: This example shows the implementation of the above-approach.
<?php
function findArea($radius) {
$pi = 3.14159;
// Formula to find area of circle
$area = $pi * $radius * $radius;
return $area;
}
function findPerimeter($radius) {
$pi = 3.14159;
// Formula to find perimeter of circle
$perimeter = 2 * $pi * $radius;
return $perimeter;
}
// Driver Code
$radius = 12;
$area = findArea($radius);
echo "Area of Circle: " . $area;
$perimeter = findPerimeter($radius);
echo "\nPerimeter of Circle: " . $perimeter;
?>
Output
Area of Circle: 452.38896 Perimeter of Circle: 75.39816
PHP Program to Find Area and Perimeter of Circle
Given a Radius of Circle, the task is to find the Area and Perimeter of Circle in PHP. To find the area, use the formula π * r2, where r is the radius of the circle. To find the perimeter (circumference), use the formula 2 * π * r.
There are two approaches to calculate the area and perimeter (circumference) of a circle using PHP:
Table of Content
- Using Functions and Constants for Pi
- Using PHP’s Math Functions