what is the difference between AND && OR ||

Discussion in 'PHP' started by ramysarwat, Jan 19, 2010.

  1. #1
    im trying to compare 4 variables so it must be look like this

    if ($a == $b and $c ==$d) or ($e==$f and $g==$h)

    but the result is always wrong
     
    ramysarwat, Jan 19, 2010 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    AND and OR are for boolean/bit comparison.

    This is probably what you want:
    if ($a == $b && $c ==$d) || ($e==$f && $g==$h)
    PHP:
     
    digitalpoint, Jan 19, 2010 IP
  3. ramysarwat

    ramysarwat Peon

    Messages:
    164
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for reply but didnt work

    error: Parse error: syntax error, unexpected T_BOOLEAN_OR
     
    ramysarwat, Jan 19, 2010 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    Should probably copy/paste the actual code and the full error message...
     
    digitalpoint, Jan 19, 2010 IP
  5. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #5
    You need to put it all in parentheses
    
    if ( ($a == $b and $c ==$d) or ($e==$f and $g==$h) )
    
    PHP:
     
    Gray Fox, Jan 19, 2010 IP
  6. CDarklock

    CDarklock Peon

    Messages:
    200
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Bit more than that, actually:

    
    if ( ( ( $a == $b ) and ( $c ==$d ) ) or ( ( $e==$f ) and ( $g==$h ) ) )
    
    PHP:
    That works, but you actually MEAN:

    
    if ( ( ( $a == $b ) && ( $c ==$d ) ) || ( ( $e==$f ) && ( $g==$h ) ) )
    
    PHP:
     
    CDarklock, Jan 19, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    Simple..

    if ($a == $b && $c == $d || $e==$f && $g==$h){
    
    //match
    }
    PHP:
     
    danx10, Jan 19, 2010 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    That's not at all true.

    The PHP operators 'and' and 'or' are identical to '&&' and '||' except that they have different association precedence. Most significantly for typical uses, '||' associates before assignment operators, while 'or' associates after them.

    For example, you can do:

    $stmt = mysql_query($query) or die(mysql_error());
    Code (markup):
    and get the desired outcome (fall through and evaluate what's after the 'or' only if the assignment that comes before it evaluates as false) but if you tried to do:

    $stmt = mysql_query($query) || die (mysql_error());
    Code (markup):
    then you'd get an undesired outcome because that is equivalent to:

    $stmt = (mysql_query($query) or die(mysql_error()));
    Code (markup):
    which would have a boolean result which makes $stmt useless.

    For bitwise comparisons you'd use the bitwise operators like '&' and '|'.
     
    SmallPotatoes, Jan 20, 2010 IP