PHP Either/Or

Discussion in 'Web Design & Coding' started by Techno Child, Mar 6, 2007.

  1. Techno Child

    Techno Child web{designer} Folding Team

    Messages:
    871
    Location:
    Myrtle Beach
    How exactly would this be done?

    I realize that this basically means if both conditions are true, then continue... How would I set this up so that if either one is true, the script will continue?

    Code:
    
    if ($a == "a" || $b == "b") {
    
    continue
    
    }else{
    
    dont continue
    
    }
    
    
    thanks
    -tc


    EDIT: oh yeah... it is pretty important that I do not split this up into two if/else statements
     
  2. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    how does that not work?

    if ($a == "a" && $b == "b")
    thats if both are true
    (btw you can use OR and AND)
     
  3. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    and only continues if both are true, he wants it to continue on all 3 eventualities. You are better off using logical OR rather than bitwise or (||)
     
    Techno Child likes this.
  4. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    what 3 eventualities? what am I looking at, it only has 2 conditions
     
  5. Techno Child

    Techno Child web{designer} Folding Team

    Messages:
    871
    Location:
    Myrtle Beach
    the third eventuality would be if both were true...

    anyways, I was not aware that I could use "AND" or "OR" call me a noob :(

    thanks guys


    EDIT:

    what would be the bitwise AND? just curious...
     
  6. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    Bitwise AND is &&
     
  7. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    Actually, bitwise AND is a single ampersand: &
    A double ampersand (&&) is a logical AND.
     
  8. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    I'm not sure I understand the original question either. The code in the first post was:

    Code:
    if ($a == "a" || $b == "b")
      // continue
    else
      // don't continue
    What that says is: if ($a == "a") is true OR ($b == "b") is true OR both conditions are true, then execute whatever is in the "continue" block. The code in the "don't continue" block is only executed if neither of the conditions are true (i.e. BOTH conditions are false). Isn't that what you want?

    Also note that short-circuit evaluation applies to the logical OR operator (||) that you're using. What that means is if ($a == "a") evaluates to true, then the code in the "continue" block will be executed regardless of what the ($b == "b") condition evaluates to.
    In other words, if the first condition in a logical OR evaluates to TRUE, the second condition is basically ignored and the whole expression evaluates to TRUE. If the first condition evaluates to FALSE, then the whole expression evaluates to whatever the second condition evaluates to. If I understand your question correctly, this shouldn't matter for your situation though.
     
    Last edited: Mar 7, 2007
  9. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    OK currently you have it setup to continue if either or both of your conditions are true to continue. Are you wanting it so that it only continues if its one or the other and not both true?

    PHP Logical Operators: You might want to look at 'xor'.

    PHP:
    if ($a == 'a' xor $b == 'b') {
        
    // continue
    } else {
        
    // dont continue
    }
     
    Techno Child likes this.
  10. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    Ah, that might be what he was after. In that case, yep, XOR should do it. :)
     
    Techno Child likes this.
  11. Techno Child

    Techno Child web{designer} Folding Team

    Messages:
    871
    Location:
    Myrtle Beach
    Actually I needed it to continue if either one or both is true... The example I have demonstrated was the current codding for a simple login script which shows/hides the rest of the page depending on the user's credentials. What I needed to add was a line that checked for a cookie that gets set on the previous page, however I fixed it by separating the login form from the actual content page.

    This info is good to know though, thanks for the link Geffy... Being self taught, I had no idea that these were called logical operators, so I couldn't look it up on their site.

    -tc