PHP:if(!$ one == $ two)总是不起作用?

Kar*_*rem 1 php operators operator-precedence

是的,这只是一个我想得到答案的问题.我经历了几次,其中:

if(!$one == $two){ echo "Not the same"; }else{ echo "The same"; }
Run Code Online (Sandbox Code Playgroud)

不会工作,而且

if($one == $two){ echo "The same"; }else{ echo "Not the same"; }
Run Code Online (Sandbox Code Playgroud)

将工作.

为什么有时不起作用?当第一个不起作用时,我总是需要像第二个那样重新编码.

Chr*_*isM 5

你需要写

if(!($one == $two))
Run Code Online (Sandbox Code Playgroud)

要么

if($one != $two)
Run Code Online (Sandbox Code Playgroud)

自从!运算符的优先级高于==运算符.

另见:http://www.php.net/manual/en/language.operators.precedence.php


cod*_*ict 5

!是具有较高precedence==,所以你应该使用括号为:

if(!($one == $two))
Run Code Online (Sandbox Code Playgroud)