Jump to content

Change output if integer is less than/more than x


DomenicF

Recommended Posts

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  :shy:

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.