Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.