Simple PHP Program

PHP (Hypertext Preprocessor) is basically Perl code that runs on a webserver. It takes input entered by the user from forms which are on HTML webpages and does stuff with them and outputs the results. It’s sometimes called CGI scripting too (Common Gateway Interface) if it uses that older service as the two are closely related, but PHP offers a more direct way to accomplish the same things so cgi is considered obsolete by most people.

To get the PHP script working you write it and upload it to a server that allows PHP (most paid for webhosts allow it, free ones often do not for security reasons) and you write an HTML file to interact with it. Pretty simple really. Here’s an HTML file that will interact with a PHP script to convert inches and centimeters to each other; you would just need to save it as an html file and open it in your browser, but you’ll have to change the address of the script to the correct server and folder you will eventually upload the PHP file to…
<html>
<body>

Enter inches to convert to centimeters
<form action=”theaddressofthescript.com/thescript.php” method=”post”>

<input type=”double” name=”inches” size=”30″/>
<input type=”submit” value=”Submit”/>
<br/>
or…<br/>
Enter centimeters to convert to inches
<br/>
<input type=”double” name=”centimeters” size=”30″/>
<input type=”submit” value=”Submit”/>
</body>
</html>

But to make it work you upload this script to a webserver and folder on that webserver matching the address in the last file…

<html>
<body>
<?php
$inches = $_POST[‘inches’];//_POST is a variable array php uses which stores

//all data transferred during a post action. It’s indexed by the name of the input created in the previous file which is ‘inches’ in this case
$centimeters = $_POST[‘centimeters’];//same thing, this time indexed by ‘centimeters’
echo $inches . ” inches are equal to ” . (double)($inches * 2.54) . ” centimeters”;
echo “<br/>”;
echo $centimeters . ” centimeters are equal to ” . (double)($centimeters / 2.54) . ” centimeters”;
?>

</body>
</html>

You can see it working on my webserver below. Try it out and hit the backbutton on your webbrowser to return here afterwards!

Enter inches to convert to centimeters


or…
Enter centimeters to convert to inches