Ad space (banner)
๐Ÿ˜PHP Lessons
Lesson 66 / 68

Converting Temperature (Celsius to Fahrenheit)

In this lesson you will convert between Celsius and Fahrenheit, practising how to turn a formula straight into code. This is for anyone searching for "PHP Celsius Fahrenheit conversion".

Celsius to Fahrenheit is "C x 9/5 + 32", and Fahrenheit to Celsius is "(F - 32) x 5/9". Unit conversion is a classic programming exercise.

The sample code turns each formula directly into code as celsiusToFahrenheit and fahrenheitToCelsius. Seeing 25 C become 77 F and that 77 F convert back to 25 C makes the round trip click.

A common stumbling block for beginners is getting the order of the division or the operator precedence wrong and landing on the wrong answer. Using parentheses to make the order explicit is the easiest way to avoid that.

It is a classic conversion exercise: simple formulas turned straight into code, with good practice at keeping types in mind. Special values such as absolute zero (-273.15 C) go through exactly the same formula.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run PHP directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)