In this article I would like to share with you how to compute the area of the circle and its circumference using PHP as our programming language. What the program will do is to ask the user to enter the radius of the circle and then compute the corresponding area and circumference of the circle in meters. I hope you will find my work useful in your learning in PHP programming.
If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360.
Sample Output of Our Program
Program Listing
<html>
<title> Area of a Circle Calculator</title>
<style>
body {
font-size:20px;
font-family:arial;
color:blue;
}
</style>
<?php
error_reporting(0);
$values = $_POST['value'];
$radius = (float)$values;
$circle = $radius * 2 * pi();
$area = pow($radius, 2) * pi();
if(isset($_POST['check'])) {
if (ctype_alpha($values)) {
echo "<script>";
echo "alert('PLEASE ENTER A NUMERIC VALUE.');";
echo "</script>";
$values="";
}
else if(preg_match('#[^a-zA-Z0-9]#', $values)) {
echo "<script>";
echo "alert('PLEASE ENTER A NUMERIC VALUE.');";
echo "</script>";
$values="";
}
else if ($values=="") {
echo "<script>";
echo "alert('It Cannot Be Empty!!! Please enter a integer value.');";
echo "</script>";
$values="";
}
else {
$title .= "The radius of the circle is " .$values.".<br><br>";
$title .= "The circumference of the circle is: "
.number_format($circle,2,'.','')." m <br>";
$title .= "The area of the circle is: ".number_format($area,2,'.','')." m²<br>";
}
}
if(isset($_POST['clear'])) {
$values=" ";
$title=" ";
}
?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
<br> <h1 align="center"> Area of a Circle Calculator</h2>
<br>
<form action="" method="post">
Enter a Radius of the Circle : <input type="text" name="value"
value="<?php echo $values; ?>" autofocus size=20/>
<input type="submit" name="check" value="Compute Area"
title="Click here to know the area of the circle."/>
<input type="submit" name="clear" value="Clear"
title="Click here to clear text box and values on the screen"/>
</form>
<br> <br>
<?php
echo $title;
?>
</body>
</html>