Vaccine fridge status
A clinic's vaccine fridge must stay between 2.0 and 8.0 degrees Celsius, both ends included. A sensor sends one reading at a time. Print TOO COLD when the reading is below 2.0, TOO WARM when it is above 8.0, and OK otherwise.
Input
One line: a number from -50 to 50 with at most one decimal place, for example 4, 7.5 or -3.2.
Output
One line: OK, TOO COLD or TOO WARM.
Example 1
Input
5
Output
OK
5 lies inside the 2.0 to 8.0 band.
Example 2
Input
1.9
Output
TOO COLD
1.9 is below the lower limit.
Example 3
Input
8.0
Output
OK
The limits themselves are acceptable; only readings strictly above 8.0 are too warm.
Constraints
- -50 <= reading <= 50
- At most one digit after the decimal point
Hints
Hint 1 of 3
Cast the input with (float) so that 1.9 is compared as a number, not as text.
Hint 2 of 3
Two comparisons are enough: check for too cold first, then for too warm, and let else handle the rest.
Hint 3 of 3
Use < and > rather than <= and >= so that 2.0 and 8.0 count as OK.
Solution
Show a reference solution and explanation
<?php
$reading = (float) trim(fgets(STDIN));
if ($reading < 2.0) {
echo "TOO COLD\n";
} elseif ($reading > 8.0) {
echo "TOO WARM\n";
} else {
echo "OK\n";
}
Why it works
An if / elseif / else chain runs the first branch whose condition is true and skips the rest, so the order of the checks decides what happens on the boundaries. Here the cold check uses < and the warm check uses >, which leaves exactly 2.0 and 8.0 for the else branch. Casting with (float) is what makes the comparison numeric: PHP 8 compares a numeric string with a number numerically anyway, but being explicit avoids surprises when the input is not clean, and it documents what the variable holds.
Lesson for this exercise: Conditions and Loops in PHP