'&&' and 'AND' is different in PHP

This question already has an answer here:

  • 'AND' vs '&&' as operator 10 answers

  • AND has lower precedence.

    $C = $A AND $B will be parsed as ($C = $A) AND $B .


    For variables $a and $b set as follows:

    <?php
    
    $a = 7;
    $b = 0;
    

    the advantage of using AND is short-circuit evaluation that occurs in code like the following:

    $c = $a AND $b;
    var_dump( $c ); // 7
    

    Since the assignment operator has higher precedence than AND, the value of $a gets assigned to $c; $b gets skipped over.

    Consider the following code in contrast:

    <?php
    
    $c = $a && $b;
    var_dump( $c ); // (bool) false
    

    In this snippet, && has a higher precedence than the assignment operator, and given the value of $a being non-zero, there is no short-circuiting. The result of this logical AND is based on the evaluation of both $a and $b. $a evaluates as true and $b as false. The overall value of the expression, false, is what gets assigned to $c. This result is quite different than in the prior example.


    both have same thing, but && has higher precedence than AND.

    for more:- 'AND' vs '&&' as operator

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

    上一篇: 转储(真和假)为布尔(真)?

    下一篇: '&&'和'AND'在PHP中是不同的