How to use PHP’s Math Functions In PHP
PHP provides built-in math functions that can be used to calculate the area and perimeter of a circle. To find the area, we use the formula M_PI * $radius * $radius, and to find perimeter, we use the formula 2 * M_PI * $radius, where M_PI is represents the mathematical pie value.
Example: This example shows the implementation of the above-explained approach.
<?php
function findArea($radius) {
$area = M_PI * $radius * $radius;
return $area;
}
function findPerimeter($radius) {
$perimeter = 2 * M_PI * $radius;
return $perimeter;
}
// Driver Code
$radius = 12;
$area = findArea($radius);
echo "Area of Circle: " . $area;
$perimeter = findPerimeter($radius);
echo "\nPerimeter of Circle: " . $area;
?>
Output
Area of Circle: 452.38934211693 Perimeter of Circle: 452.38934211693
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