1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP - Difference between "AND" and "&&"

Discussion in 'PHP' started by xemsol, Aug 23, 2011.

  1. #1
    Read some where that AND and && have some difference. After searching on the internet I cam across the answer below so I thought I should share it with all of you.
    Another purpose of sending this email is to check if this group even exists. If you disagree with the text below then please let me know the actual difference between these two.

    AND is not the same like &&

    for example:

    <?php $a && $b || $c; ?>
    is not the same like
    <?php $a AND $b || $c; ?>

    the first thing is
    (a and b) or c

    the second
    a and (b or c)

    'cause || has got a higher priority than and, but less than &&

    of course, using always [ && and || ] or [ AND and OR ] would be okay, but than you should at least respect the following:

    <?php $a = $b && $c; ?>
    <?php $a = $b AND $c; ?>


    the first code will set $a to the result of the comparison $b with $c, both have to be true, while the second code line will set $a like $b and THAN - after that - compare the success of this with the value of $c

    maybe useful for some tricky coding and helpful to prevent bugs
     
    xemsol, Aug 23, 2011 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    their functionality is the same but they differ in their order of precedence.. this link might help..

    && is higher than AND
     
    JohnnySchultz, Aug 23, 2011 IP
  3. [GotHost] James

    [GotHost] James Guest

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Really you should be using parenthesis anyway, to be certain PHP is interpreting it how you want it to.
    I'd say it's bad practice to rely on PHP for deciding in which order you wanted the conditions checked. Use parenthesis so it can only interpret it one way.
     
    [GotHost] James, Aug 23, 2011 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    I would stick with && and || just to prevent any ambiguity in your coding. Save AND and OR for database queries.
     
    jestep, Aug 23, 2011 IP
    EricBruggema likes this.
  5. thetechtalk

    thetechtalk Active Member

    Messages:
    233
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #5
    There really is no difference, but I'd say go with what jestep says. Keep AND and OR for SQL.
     
    thetechtalk, Aug 23, 2011 IP
  6. thetechtalk

    thetechtalk Active Member

    Messages:
    233
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #6
    I always use && and ||, they are just easier to type, too.
     
    thetechtalk, Aug 23, 2011 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    Precedence can change in future versions - that would break your code. Parenthetical precedence will never change.
     
    Rukbat, Aug 23, 2011 IP
  8. iBank ™

    iBank ™ Peon

    Messages:
    63
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Read the original post again if you didn't get it.
     
    iBank ™, Aug 23, 2011 IP
  9. bhandu

    bhandu Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    NET PHP SQL tutorials, references, examples for web building. ... Comparison and Logical operators are used to test for true or false. Comparison Operators. Comparison operators are used in logical statements to determine equality or difference between variables or values. Given that ... &&, and, (x < 10 && y > 1) is true ...
     
    bhandu, Aug 24, 2011 IP
  10. kanikakaminial

    kanikakaminial Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    AND and OR id use in the mysql data base queries....
     
    kanikakaminial, Aug 25, 2011 IP