Temperature Converter




<?php
if(isset($_POST['temp'])) {
  $temp = $_POST['temp'];
  $from = $_POST['from'];
  $to = $_POST['to'];

  if($from == 'fahrenheit') {
    if($to == 'celsius') {
      $converted = ($temp - 32) * 5/9;
      echo '<p>' . $temp . '&deg;F is ' . round($converted, 2) . '&deg;C</p>';
    }
    elseif($to == 'kelvin') {
      $converted = ($temp + 459.67) * 5/9;
      echo '<p>' . $temp . '&deg;F is ' . round($converted, 2) . 'K</p>';
    }
    else {
      echo '<p>Please select a valid "Convert to" option.</p>';
    }
  }
  elseif($from == 'celsius') {
    if($to == 'fahrenheit') {
      $converted = ($temp * 9/5) + 32;
      echo '<p>' . $temp . '&deg;C is ' . round($converted, 2) . '&deg;F</p>';
    }
    elseif($to == 'kelvin') {
      $converted = $temp + 273.15;
      echo '<p>' . $temp . '&deg;C is ' . round($converted, 2) . 'K</p>';
    }
    else {
      echo '<p>Please select a valid "Convert to" option.</p>';
    }
  }
  elseif($from == 'kelvin') {
    if($to == 'fahrenheit') {
      $converted = ($temp * 9/5) - 459.67;
      echo '<p>' . $temp . 'K is ' . round($converted, 2) . '&deg;F</p>';
    }
    elseif($to == 'celsius') {
      $converted = $temp - 273.15;
      echo '<p>' . $temp . 'K is ' . round($converted, 2) . '&deg;C</p>';
    }
    else {
      echo '<p>Please select a valid "Convert to" option.</p>';
    }
  }
  else {
    echo '<p>Please select a valid "Convert from" option.</p>';
  }
}
?>