Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post

    Why does this if/else statement not work

    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:

    Code:
    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.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,049
    Thanks
    2
    Thanked 317 Times in 309 Posts
    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 -
    PHP Code:
    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 -
    PHP Code:
    if (($fb_location == "RRS") || ($fb_location == "Little Rock, AR")) 
    and swap your if {...} and else {...} code.
    Last edited by CFMaBiSmAd; 06-04-2007 at 06:03 PM. Reason: more info
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Jan 2006
    Location
    California. USA
    Posts
    86
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Not sure i can see your error

    try using the elseif command
    PHP Code:
    if ($fb_location != "RRS"){
    $away="@";
    }
    elseif (
    $fb_location != "Little Rock, AR"){

    $away="@";
    }
    else

    {
        
    $away="";

    Need a Website?
    http://www.webmaking.biz
    Free Photo Hosting:
    http://www.photophatty.com

  • #4
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by CFMaBiSmAd View Post
    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 -
    PHP Code:
    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 -
    PHP Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •