You're comparing strings, not numbers. The string "6.7 °C"
is greater than the string "37.0 °C"
. When comparing strings, JavaScript compares them "character" by "character" (technically, they're not characters but code units in UTF-16, but that's a technical detail). The character "6"
is greater than the character "3"
, so the comparison doesn't have to look any further; it knows that the string starting with "6"
is "greater than" the string starting with "3"
.
I would recommend modifying the PHP to output a number rather than a string. But if you want to leave the PHP as it, you can deal with this in the JavaScript:
if ( parseFloat('<?php echo $item['temp'] ?>') >= 37 ) {
parseFloat
will parse the beginning of the string into a number (it assumes base 10), stopping at the first non-number character (in this case, the space). Note that I've changed what it's comparing to, as well, and gotten rid of the extra ()
(they were harmless, but pointless).