...

View Full Version : Why does this if/else statement not work



unrelenting
06-04-2007, 06:36 PM
I can not for the life of me figure out why this isn't working like I want it to.

I am pulling from a football schedule. The fb_location will be either "RRS", "Little Rock, AR" or some other city. The first 2 are home games. The last is always a road game.

I want this to display a @ symbol next to the location in the schedule if it is an away game. I have it working fine if I just use one comparison but if I try to get it to compare RRS or Little Rock, AR, every game shows up as away with a @ beside it.

I've tried several combinations and still same outcome. Here is the code I am using:



if (($fb_location != "RRS") || ($fb_location != "Little Rock, AR"))

{
$away="@";
}

else

{
$away="";
}


It's probably something obvious to you guys but I am self taught at this stuff so bear with me. :thumbsup:

CFMaBiSmAd
06-04-2007, 06:58 PM
Because you are using negative logic, the "!", you must switch your || to &&.

Consider the case where the variable is "Little Rock, AR", your existing code evaluates to -

if (("Little Rock, AR" != "RRS") || ("Little Rock, AR" != "Little Rock, AR"))The first term is true, which causes the @. Also, if the value is "RRS", the first term is false, but the second is true, giving the @ again. By changing to &&, you will only get the @ when both terms are true and that will only happen when the value is not "RRS" or "Little Rock, AR" ("or" in English does not necessarily mean "or" in programming.)

You could also switch it to -

if (($fb_location == "RRS") || ($fb_location == "Little Rock, AR"))and swap your if {...} and else {...} code.

crunkbbfe
06-04-2007, 06:59 PM
Not sure i can see your error

try using the elseif command


if ($fb_location != "RRS"){
$away="@";
}
elseif ($fb_location != "Little Rock, AR"){

$away="@";
}
else

{
$away="";
}

unrelenting
06-04-2007, 07:51 PM
Because you are using negative logic, the "!", you must switch your || to &&.

Consider the case where the variable is "Little Rock, AR", your existing code evaluates to -

if (("Little Rock, AR" != "RRS") || ("Little Rock, AR" != "Little Rock, AR"))The first term is true, which causes the @. Also, if the value is "RRS", the first term is false, but the second is true, giving the @ again. By changing to &&, you will only get the @ when both terms are true and that will only happen when the value is not "RRS" or "Little Rock, AR" ("or" in English does not necessarily mean "or" in programming.)

You could also switch it to -

if (($fb_location == "RRS") || ($fb_location == "Little Rock, AR"))and swap your if {...} and else {...} code.

Wow, that was some heavy thinking. So much so it put my brain on the edge of aching while trying to grasp that but I finally understand. It now works perfectly.

Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum