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

Using Functions and Constants for Pi

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
<?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

Using PHP’s Math Functions

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
<?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