DomenicF Posted September 3, 2012 Share Posted September 3, 2012 Hey guys, I'm trying to do an if elseif to change a variable if it's less than x, but it's not working correctly. It only does the elseif ($temp_f < 100) { $temp_text_col = "red"; $temp_bg_col = "black"; } part of the code. Here is the whole thing: // Here we figure out what colors the text should be for temperature. // If the temperature is more than 100, make it yellow with a red // background. if ($temp_f > 100) { $temp_text_col = "yellow"; $temp_bg_col = "red"; } // If the temperature in fahrenheit is less than 100, make it red with a // black background. elseif ($temp_f < 100) { $temp_text_col = "red"; $temp_bg_col = "black"; } elseif ($temp_f < 90) { $temp_text_col = "light_red"; $temp_bg_col = "black"; } elseif ($temp_f < 80) { $temp_text_col = "yellow"; $temp_bg_col = "black"; } elseif ($temp_f < 70) { $temp_text_col = "light_yellow"; $temp_bg_col = "white"; } elseif ($temp_f < 60) { $temp_text_col = "light_gray"; $temp_bg_col = "magenta"; } elseif ($temp_f < 40) { $temp_text_col = "light_blue"; $temp_bg_col = "light_gray"; } elseif($temp_f < 32) { $temp_text_col = "blue"; $temp_bg_col = "light_gray"; } elseif($temp_f < 16) { $temp_text_col = "blue"; $temp_bg_col = "magenta"; } elseif($temp_f < 0) { $temp_text_col = "blue"; $temp_bg_col = "black"; } else { $temp_text_col = "light_blue"; $temp_bg_col = null; } The entire file can be seen at my git: https://github.com/DomenicF/weathercli/blob/master/weather_cli.php I'm a proper nub, so please go easy on me Link to comment https://forums.phpfreaks.com/topic/267969-change-output-if-integer-is-less-thanmore-than-x/ Share on other sites More sharing options...
Christian F. Posted September 3, 2012 Share Posted September 3, 2012 What do you mean with "not working correctly"? You need to tell us what you expected, and what is actually happening. Otherwise we simply cannot help you due to the lack of information. Link to comment https://forums.phpfreaks.com/topic/267969-change-output-if-integer-is-less-thanmore-than-x/#findComment-1374973 Share on other sites More sharing options...
DomenicF Posted September 4, 2012 Author Share Posted September 4, 2012 Sorry, I guess I wasn't clear enough. What is happening is that it is executing this part of the code, but ignoring the other if's/elseif's: elseif ($temp_f < 100) { $temp_text_col = "red"; $temp_bg_col = "black"; } and $temp_f is currently 70.8. Link to comment https://forums.phpfreaks.com/topic/267969-change-output-if-integer-is-less-thanmore-than-x/#findComment-1374977 Share on other sites More sharing options...
Christian F. Posted September 4, 2012 Share Posted September 4, 2012 Of course it does, if you're confused as to why then you need to re-read the PHP manual. It is very clearly explained how if/elseif/else works in it. Link to comment https://forums.phpfreaks.com/topic/267969-change-output-if-integer-is-less-thanmore-than-x/#findComment-1375004 Share on other sites More sharing options...
DomenicF Posted September 4, 2012 Author Share Posted September 4, 2012 What should I use instead of elseif then? What do you suggest as an alternative that would give me the desired function? Link to comment https://forums.phpfreaks.com/topic/267969-change-output-if-integer-is-less-thanmore-than-x/#findComment-1375006 Share on other sites More sharing options...
Christian F. Posted September 4, 2012 Share Posted September 4, 2012 I'm not saying that you should be using something other than elseif, I'm saying that your logic itself is flawed. You need to re-examine your code, and look at what it actually does. Once you figure that out, fixing it will be trivial. That said, a switch-case could also work in this situation. However, it will not help with your logic as it is. Link to comment https://forums.phpfreaks.com/topic/267969-change-output-if-integer-is-less-thanmore-than-x/#findComment-1375008 Share on other sites More sharing options...
DomenicF Posted September 4, 2012 Author Share Posted September 4, 2012 Thanks, I figured it out. Was a pretty stupid thing I did huh? Link to comment https://forums.phpfreaks.com/topic/267969-change-output-if-integer-is-less-thanmore-than-x/#findComment-1375009 Share on other sites More sharing options...
Christian F. Posted September 4, 2012 Share Posted September 4, 2012 Hehe, you could say that, yeah. Glad I could help out though. Link to comment https://forums.phpfreaks.com/topic/267969-change-output-if-integer-is-less-thanmore-than-x/#findComment-1375011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.