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

Convert Temperature

By , About.com Guide

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.

Explore PHP / MySQL
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

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

All rights reserved.