find the volume of a sphere in php

To find the volume of a sphere in PHP, we can use the following formula:

main.php
V = (4/3) * pi * r^3
21 chars
2 lines

Where V is the volume of the sphere, r is the radius, and pi is the mathematical constant pi.

Here's the PHP code to calculate the volume of a sphere of radius r:

main.php
<?php
$r = 5; // radius of the sphere
$pi = 3.14159; // mathematical constant pi

$volume = (4/3) * $pi * pow($r, 3);

echo "The volume of the sphere is: ".$volume;
?>
168 chars
9 lines

In the above code, we have initialized the radius of the sphere as $r. We have also defined the mathematical constant pi as $pi. Then, we have used the above formula to calculate the volume of the sphere and stored it in the variable $volume. Finally, we have printed the volume of the sphere using the echo statement.

You can change the value of $r to calculate the volume of the sphere for any other radius.

related categories

gistlibby LogSnag