JS vs PHP: assignment operator precedence when used with logical-or

前端 未结 3 740
猫巷女王i
猫巷女王i 2020-12-14 19:09

(PHP has || and OR. JS only has ||.)

JS. According to MDN || has higher precedence than =<

3条回答
  •  爱一瞬间的悲伤
    2020-12-14 20:01

    As to your followup question: if ( $d = $c && $e = $b && $f = $a ) is the same as:

    $d = $c;
    if($d) {
        $e = $b;
        if($e) {
            $f = $a;
            if($f) {
                ...
            }
        }
    }
    

    I assume you know this, but some of the questions are confusing to me, so I'll mention it... = is an assignment operator, not a comparison operator. if($a = $b) doesn't check if $a and $b are the same, it makes $a equal $b, then checks if $a evaluates to true. if($a == $b) checks if the two variables are the same.

提交回复
热议问题