1. Computing

Convert Temperature

By

2 of 4

Using IF for Conversions
Convert Temperature
If you recall, our form is sending data back to itself. This means that all of our PHP will be contained in the same file we placed our form in. Continuing to work in our convert.php file, place this PHP code under the HTML we entered in our 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 our 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 our next step we will add in the other conversions so options other than celcius work.

Related Video
Convert Color Photos to Black and White in Photoshop
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Step By Steps
  5. Convert Temperature - Convert Temperature Celsius - PHP Convert Script

©2014 About.com. All rights reserved.