PHP中“不等于”运算符和!=之间的区别

时间:2021-12-24 22:25:32

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

在PHP中,!=和<>运算符之间有什么区别吗?

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.

我想没有太大的差异,但我很好奇。

6 个解决方案

#1


58  

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

在主要的Zend实现中没有任何区别。您可以从PHP语言扫描程序的Flex描述中获取它:

<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:

其中T_IS_NOT_EQUAL是生成的令牌。所以Bison解析器不区分<>和!= tokens并平等对待它们:

%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

#2


22  

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.

我不确定这是Zend实现中的错误,文档中的错误,还是PHP决定忽略优先规则的情况之一。

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

更新:文档已更新,<>和!=之间不再有任何区别。

#3


8  

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

他们是一样的。但是也有!==和===运算符测试由值和类型定义的精确相等。

#4


5  

<> means either bigger or smaller. != means not equal. They basically mean the same thing.

<>表示更大或更小。 !=表示不相等。它们基本上意味着同样的事情。

#5


4  

As everyone is saying they are identical, one from one language branch C-style/shell, one from some others including MySQL which was highly integrated in the past.

正如每个人都说他们是相同的,一个来自一个语言分支C风格/ shell,一个来自其他一些包括MySQL,过去高度集成。

<> should be considered syntactic sugar, a synonym for != which is the proper PHP style for not-equal.

<>应该被认为是语法糖,是!=的同义词,这是不正确的PHP风格。

Further emphasised by the triple character identity function !==.

三重字符识别功能进一步强调了!==。

#6


1  

<> is exactly the same as != operator since both of them are parsed as T_IS_NOT_EQUAL token.

<>与!=运算符完全相同,因为它们都被解析为T_IS_NOT_EQUAL标记。

And they have same precedence.

他们有同样的优先权。

#1


58  

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

在主要的Zend实现中没有任何区别。您可以从PHP语言扫描程序的Flex描述中获取它:

<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:

其中T_IS_NOT_EQUAL是生成的令牌。所以Bison解析器不区分<>和!= tokens并平等对待它们:

%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

#2


22  

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.

我不确定这是Zend实现中的错误,文档中的错误,还是PHP决定忽略优先规则的情况之一。

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

更新:文档已更新,<>和!=之间不再有任何区别。

#3


8  

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

他们是一样的。但是也有!==和===运算符测试由值和类型定义的精确相等。

#4


5  

<> means either bigger or smaller. != means not equal. They basically mean the same thing.

<>表示更大或更小。 !=表示不相等。它们基本上意味着同样的事情。

#5


4  

As everyone is saying they are identical, one from one language branch C-style/shell, one from some others including MySQL which was highly integrated in the past.

正如每个人都说他们是相同的,一个来自一个语言分支C风格/ shell,一个来自其他一些包括MySQL,过去高度集成。

<> should be considered syntactic sugar, a synonym for != which is the proper PHP style for not-equal.

<>应该被认为是语法糖,是!=的同义词,这是不正确的PHP风格。

Further emphasised by the triple character identity function !==.

三重字符识别功能进一步强调了!==。

#6


1  

<> is exactly the same as != operator since both of them are parsed as T_IS_NOT_EQUAL token.

<>与!=运算符完全相同,因为它们都被解析为T_IS_NOT_EQUAL标记。

And they have same precedence.

他们有同样的优先权。