find the surface area of a sphere in php

To calculate the surface area of a sphere in PHP, we can use the following formula:

main.php
surface_area = 4 * pi * radius^2
33 chars
2 lines

Where pi is a constant with the approximate value of 3.14159, and radius is the radius of the sphere.

Here's the PHP code for calculating the surface area of a sphere:

main.php
<?php
$radius = 5; // Set the radius of the sphere
$pi = 3.14159; // Set the value of pi

// Calculate the surface area of the sphere
$surface_area = 4 * $pi * pow($radius, 2);

echo "The surface area of the sphere is: " . $surface_area . " square units";
?>
259 chars
10 lines

In this example, we set the radius of the sphere to 5 units and pi to the approximate value. We then use the pow function to raise the radius to the power of 2, and multiply the result by 4 and pi to get the surface area. Finally, we print out the result using the echo statement.

The output of this code will be:

main.php
The surface area of the sphere is: 314.159 square units
56 chars
2 lines

gistlibby LogSnag