Difference between "not equal" operators <> and != in PHP

In PHP, is there any difference between the != and <> operators?

In the manual, it states:

$a != $b    Not equal   TRUE if $a is not equal to $b after type juggling.
$a <> $b    Not equal   TRUE if $a is not equal to $b after type juggling.

I guess there are no huge differences but I'm curious.


In the main Zend implementation there is not any difference. You can get it from the Flex description of the PHP language scanner:

<ST_IN_SCRIPTING>"!="|"<>" {
    return T_IS_NOT_EQUAL;
}

Where T_IS_NOT_EQUAL is the generated token. So the Bison parser does not distinguish between <> and != tokens and treats them equally:

%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL

As the accepted answer points out the implementation is identical, however there is a subtle difference between them in the documentation...

According to this page the <> operator has slightly higher precedence than != .

I'm not sure if this is a bug in the Zend implementation, a bug in the documentation, or just one of those cases where PHP decides to ignore the precedence rules.

Update: The documentation is updated and there is no longer any difference between <> and != .


They are the same. However there are also !== and === operators which test for exact equality, defined by value and type.

链接地址: http://www.djcxy.com/p/10050.html

上一篇: 如何在MySQL中插入“如果不存在”?

下一篇: PHP中“不相等”运算符<>和!=之间的区别