Scientific PHP Math Functions

I was bored the other night and decided to write some math functions in PHP that might be helpful for your school’s science class. I wrote 4 functions, so if you need a quick way of finding the answers, stick these PHP snippets on a PHP web server and run the scripts with the needed variables edited for your needs.

This script calculates the effects of gravity on an object and how long it will take for it to fall in PHP.

<?
//Calculates effect of gravity on objects from height in centimeters (defined in the variable H)
$H = 1000;
$multiplication = 2 * $H;
$division = $multiplication / 980;
print("Seconds taken to fall from $H centimeter(s): ");
echo(sqrt($division) . "<br />");
?>

This script calculates potential energy in an object in PHP.

<?
//Calculates Potential Energy (M defines mass in KG, G defines gravity, H defines height in meters)
$M = 58.9670;
$G = 9.8;
$H = 50;
$multiplication = $M * $G * $H;
print("Potential Energy in object $M kg with $G gravity from $H meters high: ");
echo(($multiplication) . " Joules<br />");
?>

This script calculates the momentum of an object in PHP.

<?
//Calculates Momentum (M defines mass in KG and V defines velocity)
$M = 58.9670;
$V = 25;
$multiplication = $M * $V;
print("Momentum of an object with a mass of $M and a velocity of $V : ");
echo(($multiplication) . " Newtons<br />");
?>

This script calculates the kinetic energy in an object in PHP.

<?
//Calculates Kinetic Energy (M defines mass in KG and V defines velocity)
$M = 58.9670;
$V = 25;
$velocitysquared = $V * $V;
$division = $M / 1/2;
$finalmultiply = $velocitysquared * $division;
print("Kinetic Energy in $M kg with a velocity of $V meters per second squared: ");
echo(($finalmultiply) . " Joules<br />");
?>

Don’t run all this code in the same file because it will result in a PHP error.


This entry was posted on Sunday, June 22nd, 2008 at 9:41 pm and is filed under Computers, PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply