Concept

How can PHP convert a Fahrenheit temperature to Celsius?

ComputerScienceOne / Converting Units

"To convert a temperature from degrees Fahrenheit to degrees Celsius, use the formula C = (F − 32) · 5/9. Begin with the basic PHP script shell containing the opening and closing PHP tags and comments documenting the purpose of the script. It is common for programmers to use a comment with a TODO note as a reminder of work that still needs to be done. The program can be outlined as follows: first prompt the user for a temperature in Fahrenheit; next read the user’s input and use a conversion function to ensure it is a floating-point number; then calculate the degrees Celsius using the formula; and lastly print the result to the user. The program needs one variable to hold the Fahrenheit input value and another to hold the Celsius output value. The Fahrenheit value can be read and cleaned with $fahrenheit = trim(fgets(STDIN)); and converted to a floating-point value with $fahrenheit = floatval($fahrenheit); The Celsius value is computed with $celsius = ($fahrenheit - 32) * (5 / 9); Finally, printf() outputs the result: printf(\"%f Fahrenheit is %f Celsius\n\n\", $fahrenheit, $celsius). The full program is: <?php /** * This program converts Fahrenheit temperatures to * Celsius */ //1. Prompt the user for input in Fahrenheit printf(\"Please enter degrees in Fahrenheit: \"); //2. Read the Fahrenheit value from the standard input $fahrenheit = trim(fgets(STDIN)); $fahrenheit = floatval($fahrenheit); //3. Compute the degrees Celsius $celsius = ($fahrenheit - 32) * (5/9); //4. Print the result to the user printf(\"%f Fahrenheit is %f Celsius\n\n\", $fahrenheit, $celsius); ?>"

Related Ideas

How can PHP convert a Fahrenheit temperature to Celsius? | ComputerScienceOne | Bifalgorithm | Bifalgorithm