&& and 'and' are the same in php?

This question already has an answer here:

  • 'AND' vs '&&' as operator 10 answers
  • PHP - and / or keywords 4 answers

  • They do not have the same precedence. Fully parenthesised, your code is:

    ($keyWord = 2) and (3>4); // $keyWord = 2
    $symbol = (2 && (3>4)); // $symbol = false
    

    2 and false are clearly not the same, hence 'not-equal' .

    More on operator precedence


    Well, altering the code slightly to:

    <?php
    $keyWord =  2 and 3>4;
    
    $symbol  =  2 &&  3>4;
    
    var_dump($keyWord);
    var_dump($symbol);
    ?>
    

    produces an output of:

    int(2) bool(false)
    

    As Kolink points out above, this is due to operator precedence.


    Here is the precedence of logical operators (part of the table)

    left    &&  logical
    left    ||  logical
    left    ? :     ternary
    right   = += -= *= /= .= %= &= |= ^= <<= >>= =>     assignment
    left    and
    

    In your case :

    case 1:
    
    $keyWord =  2 and 3>4;
    
    ($keyWord =  2) and (3>4);
    

    Here $keyWord = 2

    case 2:
    
    $symbol  =  2 &&  3>4;
    
    $symbol  =  (2 && (3>4));
    

    Here $symbol = false

    Solution : $keyWord = (2 and (3>4)); and $symbol = 2 && (3>4); Use brackets

    Ref: http://php.net/manual/en/language.operators.precedence.php

    链接地址: http://www.djcxy.com/p/57608.html

    上一篇: PHP运算符&& vs AND,

    下一篇: &&和'和'在php中是一样的吗?