Convert Temperature With This PHP Script

Close-Up Of Thermometer By Wood
Brandy Arivett/EyeEm/Getty Images

This PHP script can be used to convert temperature values to or from Celsius, Fahrenheit, Kelvin and Rankine. Follow this step-by-step tutorial and create your own temperature conversion program.

01
of 04

Setting Up the Form

The first step in creating an online temperature conversion program is to collect the data from the user. In this case, the form collects the degrees and the units the degrees are measured in. You are using a drop-down menu for the units and giving them four options. This form uses the ​​$_SERVER[’PHP_SELF’] command to indicate it sends the data back to itself.

Put the code below into a file called convert.php

 

 <head>
<title>Convert Temperature</title>
</head>
<body>
<h2>Temperature Conversion</h2>
<form action = "<?php echo $_SERVER[’PHP_SELF’]; ?>"
method = "GET">
Degrees:
<input type = "text" name = "degree" size=4>
<select name="scale"> <option value="celcius">Celsius</option> <option value="fahrenheit">Fahrenheit</option> <option value="kelvin">Kelvin</option> <option value="rankine">Rankine</option> </select>
<br/>
<input type = "submit" name = "Convert Temperature"/>
</form> 
02
of 04

Using IF for Conversions

If you recall, the form is sending data back to itself. This means that all of your PHP will be contained in the same file you placed your form in. Continuing to work in the convert.php file, place this PHP code under the HTML you entered in the last step.

 

 <?php
if ($scale == "celcius")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>celsius</td></tr>";
$c_2_f = $degree*9/5+32;
print "<tr><td>$c_2_f</td><td>fahrenheit</td></tr>";
$c_2_k = $degree+273.15;
print "<tr><td>$c_2_k </td><td>kelvin</td></tr>";
$c_2_r = $c_2_f+459.6;
print "<tr><td>$c_2_r</td><td>rankine</td></tr></table>";}
?> 

This code converts a Celcius temperature to Fahrenheit, Kelvin and Rankine and then prints their values in a table below the original form. The form is still at the top of the page and is ready to accept new data. Currently, if the data is anything but Celcius it will be ignored. In the next step, you will add in the other conversions so options other than Celcius work.

03
of 04

Adding More Conversions

Still working in the convert.php file, add the following code at the end of the document, just before the ?> end PHP tag.

 

 if ($scale == "fahrenheit")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>farhenheit</td></tr>";
$f_2_c = ($degree -32)*5/9;
print "<tr><td>$f_2_c</td><td>celsius</td></tr>";
$f_2_k = $f_2_c+273.15;
print "<tr><td>$f_2_k </td><td>kelvin</td></tr>";
$f_2_r = $degree+459.6;
print "<tr><td>$f_2_r</td><td>rankine</td></tr></table>";}
if ($scale == "kelvin")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>
$degree</td><td>kelvin</td></tr>";
$k_2_f = ($degree - 273.15) * 9 / 5 + 32;
print "<tr><td>$k_2_f</td><td>fahrenheit</td></tr>";
$k_2_c = $degree-273.15;
print "<tr><td>$k_2_c </td><td>celsius</td></tr>";
$k_2_r = $k_2_f+459.6;
print "<tr><td>$k_2_r</td><td>rankine</td></tr></table>";}
if ($scale == "rankine")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>rankine</td></tr>";
$r_2_f = $degree-459.6;
print "<tr><td>$r_2_f</td><td>fahrenheit</td></tr>";
$r_2_c = ($r_2_f - 32)*5/9;
print "<tr><td>$r_2_c </td><td>celsius</td></tr>";
$r_2_k = $r_2_c + 273.15;
print "<tr><td>$r_2_k</td><td>kelvin</td></tr></table>";} 

and put this code after the ?> closing PHP tag to close up the HTML

 

</body>
</html> 
04
of 04

The Script Explained

First, the script collects data from the user and then submits this information to itself. When the page reloads after hitting submit, the PHP at the bottom now has variables to work with and can execute.

Your convert temperature PHP consists of four IF statements, one for each of the unit measurements available on our form. The PHP then makes the appropriate conversions based on the users choice and outputs a table. The complete code for this script can be downloaded from GitHub.