Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- - Temperature Conversion
- - Converts one temperature to another
- $from - The temperature to convert from.
- => "c" - celsius
- => "f" - fahrenheit
- => "k" - kelvin
- $to - The temperature to convert to.
- => "c" - celsius
- => "f" - fahrenheit
- => "k" - kelvin
- $value - The temperature to be converted
- */
- function convertTemperature($from, $to, $value)
- {
- $from = strtolower($from);
- $to = strtolower($to);
- if(!in_array($from, array("f", "c", "k") )) die("Invalid From value");
- if(!in_array($to, array("f", "c", "k") )) die("Invalid To value");
- if (is_numeric($value) || is_double($value))
- {
- // Get input value in degree Kelvin, if($from == "K") then no action required.
- if($from == "c") $value += 273.15;
- if($from == "f") $value = (($value - 32) / 1.8) + 273.15;
- if($value<0) die("Invalid input value entered."); // Validating received value.
- // GET converted value, if($to == "K") // No acton required.
- if($to == "c") $value -= 273.15;
- if($to == "f") $value = (($value - 273.15) * 1.8) + 32;
- return $value;
- }
- else
- {
- return false;
- }
- }
- // for example.
- echo convertTemperature("c", "f", 39);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement