How to Find Area of a Circle with Diameter in PHP | Functions
How to find area of a circle with diameter in PHP using functions.
In this article, you will learn how to find area of a circle with diameter in PHP using functions.
Example
Diameter = 24 units
Circumference = 75.36 units
Area = 452.16 sq. units
You should have knowledge of the following topics in PHP programming to understand this program:
- PHP
echo
andprint
statement
1. By using the default value of PI = 3.14
<?php
$r = 12; $d = NULL; $c = NULL; $a = NULL;
// $r = radius, $d = diameter, $c = circumference, $a = area
// It will calculate the diameter, circumference, and area
$d = 2 * $r;
$c = 2 * 3.14 * $r;
$a = 3.14 * ($r * $r);
echo "\nDiameter = " . $d . " units";
echo "\nCircumference = " . $c . " units";
echo "\nArea = " . $a . " sq. units";
?>
Output
Diameter = 24 units
Circumference = 75.36 units
Area = 452.16 sq. units
Explanation
In the above program, we have set the input variable $r with the value 12
. Then we applied the standard formula to calculate the diameter, circumference, and area.
Standard formulas are given at the top of the article near the example portion can see there.
After the whole calculation, It will return these values following:
- Diameter = 24 units
- Circumference = 75.36 units
- Area = 452.16 sq. units
2. By using the pi() function
<?php
$r = 12; $d = NULL; $c = NULL; $a = NULL;
// $r = radius, $d = diameter, $c = circumference, $a = area
// It will calculate the diameter, circumference, and area
$d = 2 * $r;
$c = 2 * pi() * $r;
$a = pi() * pow($r, 2);
echo "\nDiameter = " . $d . " units";
echo "\nCircumference = " . $c . " units";
echo "\nArea = " . $a . " sq. units";
?>
Output
Diameter = 24 units
Circumference = 75.398223686155 units
Area = 452.38934211693 sq. units
How to Find Area of a Circle with Diameter in PHP using Functions
<?php
// To calculate the diameter
function diameter($r) {
return 2 * $r;
}
// To calculate the circumference
function circumference($r) {
return 2 * 3.14 * $r;
}
// To calculate the area
function area($r) {
return 3.14 * ($r * $r);
}
$r = 12; // $r = radius
echo "\nDiameter = " . diameter($r) . " units";
echo "\nCircumference = " . circumference($r) . " units";
echo "\nArea = " . area($r) . " sq. units";
?>