可能重复: 'AND'和'&&'作为运算符
对不起,基本的问题,但我开始学习PHP只是一个星期前,并找不到在谷歌/ stackoverflow这个问题的答案。
我经历了下面的程序:
$one = true; $two = null; $a = isset($one) && isset($two); $b = isset($one) and isset($two); echo $a.'<br>'; echo $b;它的输出是:
false true我读&& /和是一样的。 两者的结果有何不同? 有人能说出真实的原因吗?
Possible Duplicate: 'AND' vs '&&' as operator
Sorry for very basic question but I started learning PHP just a week ago & couldn't find an answer to this question on google/stackoverflow.
I went through below program:
$one = true; $two = null; $a = isset($one) && isset($two); $b = isset($one) and isset($two); echo $a.'<br>'; echo $b;Its output is:
false trueI read &&/and are same. How is the result different for both of them? Can someone tell the real reason please?
最满意答案
原因是运营商优先 。 在您使用&& and & =三个运算符中,优先顺序是
&& = and因此,程序中的$b按照预期进行计算,但对于$b ,首先计算语句$b = isset($one) ,从而给出意想不到的结果。 它可以如下修复。
$b = (isset($one) and isset($two));The reason is operator precedence. Among three operators you used &&, and & =, precedence order is
&& = andSo $a in your program calculated as expected but for $b, statement $b = isset($one) was calculated first, giving unexpected result. It can be fixed as follow.
$b = (isset($one) and isset($two));更多推荐
发布评论