Applied Programming/Functions/PHP

variables.php

edit
<?php

// This program converts a Fahrenheit temperature to Celsius.
//
// Input:
//     Fahrenheit temperature
//
// Output:
//     Fahrenheit temperature
//     Celsius temperature
//
// Example:
//     Enter Fahrenheit temperature:
//     122
//     122 Fahrenheit is 50 Celsius
//    
// References:
//     * http://www.mathsisfun.com/temperature-conversion.html
//     * http://php.net/manual/en/features.commandline.io-streams.php
//     * https://stackoverflow.com/questions/481466/php-string-to-float


function getFahrenheit() {
  echo ("Enter Fahrenheit temperature:");
  $fahrenheit = (float) rtrim(fgets(STDIN));
  return $fahrenheit;
}

function convertToCelsius($fahrenheit) {
  $temperature_difference = 32;
  $temperature_ratio = 5 / 9;
  $celsius = ($fahrenheit - $temperature_difference) * $temperature_ratio;
  return $celsius;
}

function displayOutput($fahrenheit, $celsius) {
  echo $fahrenheit . " Fahrenheit is " . $celsius . " Celsius!";
}

function main() {
  $fahrenheit = getFahrenheit();
  $celsius = convertToCelsius($fahrenheit);
  displayOutput($fahrenheit, $celsius);
}

main();

?>

Try It

edit

Copy and paste the code above into one of the following free online development environments or use your own PHP compiler / interpreter / IDE.

See Also

edit