PHP: if(!$one == $two) 并不总是有效?
是的,这只是我想得到答案的一个问题。我经历过几次,其中:
if(!$one == $two){ echo "Not the same"; }else{ echo "The same"; }
不会起作用,并且
if($one == $two){ echo "The same"; }else{ echo "Not the same"; }
会起作用。
为什么有时不起作用?当第一个不起作用时,我总是需要像第二个一样重新编码。
Yes, this is just a question i would like to get an answer on. I experienced it a couple of times, where this:
if(!$one == $two){ echo "Not the same"; }else{ echo "The same"; }
Will not work, and
if($one == $two){ echo "The same"; }else{ echo "Not the same"; }
will work.
Why doesn't it work sometimes? I always need to recode like the second, when the first doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要
这是因为没有括号,它会检查 $one 是否为 false,然后检查 $two == $one 是否。以下是唯一一次不使用括号即可工作的情况。评估 if (true == true) as !$one = true。
You need
This is because without the brackets, it is checking if $one is false and then checking if $two == $one. The following is the only time that it will work without the brackets. Evaluating to if (true == true) as !$one = true.
你需要写
或
因为 !运算符的优先级高于 == 运算符。
另请参阅: http://www.php.net/manual/en /language.operators.precedence.php
You need to write
or
since the ! operator has a higher precedence than the == operator.
See also: http://www.php.net/manual/en/language.operators.precedence.php
!
具有更高的优先级
比==
所以你应该使用括号:!
is having higherprecedence
than==
so you should use parenthesis as: