1. Home
  2. Computing & Technology
  3. PHP / MySQL

Convert Temperature

By Angela Bradley, About.com

2 of 4

Using IF for Conversions

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.

Index: Convert Temperature

  1. Setting Up the Form
  2. Using IF for Conversions
  3. Adding More Conversions
  4. The Script Explained

2 of 4

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Convert Temperature - Convert Temperature Celsius - PHP Convert Script

©2008 About.com, a part of The New York Times Company.

All rights reserved.