为什么这个条件语句的变量前面有一个感叹号?


Why is there an exclamation point before the variable on this conditional statement?

这是条件语句:

$comments = get_value_of_comments_as_string();
if( !$comments == "on" ){...}

$comments变量前的!点的目的是什么?特别是$comments保存了一个字符串。

编辑:这个条件似乎不是一个打字错误,因为原始编码器有5个这样的条件语句。

!是逻辑否定运算符(或不是)所以基本上它将真值更改为假,将假值更改为真。

我敢肯定作者的意图是

!($comments == "on") // if comments == "on" return false

但是他实际上说的是

(!$comments) == "on")  // if not comments == "on" ... this test will only succeed if comments is null or an empty string.  

更好的表达方式是

$comments != "on"

这个行为是因为!具有比==更高的优先级,因此它将在==之前求值。

相关文章: