php if-else势
代码:
<?php
$a = 200;
$b = 300;
if ($a > $b + $b != 3)
print "Correct";
else
print "Incorrect";
?>
输出是:正确
有人可以帮助我理解为什么输出变得“正确”?
Code:
<?php
$a = 200;
$b = 300;
if ($a > $b + $b != 3)
print "Correct";
else
print "Incorrect";
?>
output is: Correct
Can someone help me understand why the output became "Correct"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要了解这里发生的事情,您需要查看操作员预定首先要评估什么。它不是向右。 if语句中运算符的顺序如下:
+ - ++ - 〜(int)(float)(string)(arnay)(array)(object)(bool) @
- arithmetic(Unary) + and-),增量/减少,位,类型铸造和错误控制&lt; &lt; =&gt; &gt; =
- 关联比较==!===== ==&lt;&gt; &lt; =&gt;
- 非关联比较本质上,您的if语句对此进行了分解:
随着您的价值观
,当然,false不是3,并且使您的if语句正确。如果您想评估
200大于300和300不是3
,那么您需要逻辑和操作员,或&amp;&amp;
,它将打印出来
不正确
To understand what is happening here, you need to look at the list of operator precendence to see what is being evaluated first. It's not left to right. The order of the operators in your if statement are as follows:
+ - ++ -- ~ (int) (float) (string) (array) (object) (bool) @
- arithmetic (unary + and -), increment/decrement, bitwise, type casting and error control< <= > >=
- associative comparison== != === !== <> <=>
- non associative comparisonSo in essence, your if statement breaks down to this:
With your values becomes
So of course, false is not 3, and makes your if statement correct. If you want to evaluation
200 is greater than 300 AND 300 is not 3
, then you need the logical AND operator, or&&
, which would bewhich would print
Incorrect