和!= [duplicate]有什么区别?

时间:2021-09-28 13:58:39

This question already has an answer here:

这个问题已经有了答案:

In PHP to check non-equality (without checking type) you can do this:

在PHP中检查不相等(不检查类型),您可以这样做:

if( A != B ) {
    DO SOMETHING;
}

But you can also do this, which has the same result:

但是你也可以这样做,结果是一样的:

if( A <> B ) {
    DO SOMETHING;
}

Is there any difference?

有什么不同吗?

Does using != over <> change the evaluation in any way, shape, or form?

使用!= / <>是否以任何方式、形状或形式改变评估?

7 个解决方案

#1


62  

Forgetting documentation for a minute, let's check out the source code. Let's start with the scanner (lexer):

暂时忘记文档,让我们检查一下源代码。让我们从扫描仪(lexer)开始:

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

So they parse to the same token. Let's check out the parser:

所以它们解析到同一个记号。让我们检查解析器:

expr T_IS_NOT_EQUAL expr { zend_do_binary_op(ZEND_IS_NOT_EQUAL, &$$, &$1, &$3 TSRMLS_CC); }

So we know that the opcode that's fired is ZEND_IS_NOT_EQUAL...

我们知道触发的操作码是ZEND_IS_NOT_EQUAL…

Now, let's check out the operation:

现在,让我们来看看手术:

static int ZEND_FASTCALL  ZEND_IS_NOT_EQUAL_SPEC_CONST_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
    USE_OPLINE

    zval *result = &EX_T(opline->result.var).tmp_var;

    SAVE_OPLINE();
    ZVAL_BOOL(result, fast_not_equal_function(result,
        opline->op1.zv,
        opline->op2.zv TSRMLS_CC));

    CHECK_EXCEPTION();
    ZEND_VM_NEXT_OPCODE();
}

So there's literally no difference. Since they parse to the same token, they have exactly the same precedence (so the docs are either wrong or misleading). Since they use the same executor, and there's no decision point in the opcode routine, they execute identical code.

所以没有区别。由于它们解析到相同的标记,所以它们的优先级完全相同(因此文档要么是错误的,要么是错误的)。由于它们使用相同的执行程序,并且在操作码例程中没有决策点,所以它们执行相同的代码。

So yes, <> and != are 100% interchangeable, and there's absolutely no technical reason to use one over the other.

所以,是的,<>和!=是100%可互换的,而且绝对没有技术上的理由使用其中之一。

With that said, there is something significant to gain by being consistent. So I'd recommend just sticking with != and being done with it...

话虽如此,保持一致是有意义的。所以我建议坚持下去!

Edit

I've updated the docs to reflect this, and fixed another issue with the precedence order (++ and -- have the same precedence as casting). Check it out on docs.php.net

我已经更新了文档以反映这一点,并修复了另一个具有优先级顺序(++和——与强制转换具有相同优先级)的问题。在docs.php.net上查看。

#2


27  

No difference.

没有区别。

However, != allows the convenience of more easily adding an extra = to force type comparison.

但是,!=允许更方便地添加一个额外的=来强制类型比较。

#3


26  

One's old, one's new.

一个旧的,一个是新的。

according to the manual:

根据手册:

$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.

use !=.

使用! =。

The minor difference: there is also an order of precedence. look here.

小的区别:也有先后顺序。看这里。

<> comes before != in the precedence table, but they accomplish the exact same thing.

<>出现在优先表的前面!=,但是它们完成了完全相同的事情。

#4


5  

As mentioned at the documentation website, <>and !=are just synonyms. That means they are completely interchangeable. The history of php is a bit wild, so naming conventions, even to the point how operators are to be called, were and still are not really unified.

正如在文档网站上提到的,<>和!=只是同义词。这意味着它们是完全可以互换的。php的历史有点复杂,所以命名约定,甚至到如何调用操作符,过去和现在都不是真正统一的。

#5


2  

According to PHP manual: http://fr.php.net/manual/en/language.operators.comparison.php it does not seem to have any difference.

根据PHP手册:http://fr.php.net/manual/en/language.operators.org/。PHP似乎没有任何区别。

#6


2  

There is no difference. I guess <> is something that was added in a later version of php. Kind of reminds me of Python. I think it the same with using AND or && for the and operator

没有区别。我猜<>是在后来的php版本中添加的。这让我想起了Python。我认为对于AND运算符的使用和&&也是一样的

#7


0  

It isn't any different, but I think I remember != was faster once, because I ran a test and found out <> was executing the "diff" methods of the objects I was comparing, which can be slower than the "compare" methods.

这没有什么不同,但是我记得!=的速度更快,因为我运行了一个测试,发现<>正在执行我正在比较的对象的“diff”方法,这比“compare”方法要慢。

#1


62  

Forgetting documentation for a minute, let's check out the source code. Let's start with the scanner (lexer):

暂时忘记文档,让我们检查一下源代码。让我们从扫描仪(lexer)开始:

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

So they parse to the same token. Let's check out the parser:

所以它们解析到同一个记号。让我们检查解析器:

expr T_IS_NOT_EQUAL expr { zend_do_binary_op(ZEND_IS_NOT_EQUAL, &$$, &$1, &$3 TSRMLS_CC); }

So we know that the opcode that's fired is ZEND_IS_NOT_EQUAL...

我们知道触发的操作码是ZEND_IS_NOT_EQUAL…

Now, let's check out the operation:

现在,让我们来看看手术:

static int ZEND_FASTCALL  ZEND_IS_NOT_EQUAL_SPEC_CONST_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
    USE_OPLINE

    zval *result = &EX_T(opline->result.var).tmp_var;

    SAVE_OPLINE();
    ZVAL_BOOL(result, fast_not_equal_function(result,
        opline->op1.zv,
        opline->op2.zv TSRMLS_CC));

    CHECK_EXCEPTION();
    ZEND_VM_NEXT_OPCODE();
}

So there's literally no difference. Since they parse to the same token, they have exactly the same precedence (so the docs are either wrong or misleading). Since they use the same executor, and there's no decision point in the opcode routine, they execute identical code.

所以没有区别。由于它们解析到相同的标记,所以它们的优先级完全相同(因此文档要么是错误的,要么是错误的)。由于它们使用相同的执行程序,并且在操作码例程中没有决策点,所以它们执行相同的代码。

So yes, <> and != are 100% interchangeable, and there's absolutely no technical reason to use one over the other.

所以,是的,<>和!=是100%可互换的,而且绝对没有技术上的理由使用其中之一。

With that said, there is something significant to gain by being consistent. So I'd recommend just sticking with != and being done with it...

话虽如此,保持一致是有意义的。所以我建议坚持下去!

Edit

I've updated the docs to reflect this, and fixed another issue with the precedence order (++ and -- have the same precedence as casting). Check it out on docs.php.net

我已经更新了文档以反映这一点,并修复了另一个具有优先级顺序(++和——与强制转换具有相同优先级)的问题。在docs.php.net上查看。

#2


27  

No difference.

没有区别。

However, != allows the convenience of more easily adding an extra = to force type comparison.

但是,!=允许更方便地添加一个额外的=来强制类型比较。

#3


26  

One's old, one's new.

一个旧的,一个是新的。

according to the manual:

根据手册:

$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.

use !=.

使用! =。

The minor difference: there is also an order of precedence. look here.

小的区别:也有先后顺序。看这里。

<> comes before != in the precedence table, but they accomplish the exact same thing.

<>出现在优先表的前面!=,但是它们完成了完全相同的事情。

#4


5  

As mentioned at the documentation website, <>and !=are just synonyms. That means they are completely interchangeable. The history of php is a bit wild, so naming conventions, even to the point how operators are to be called, were and still are not really unified.

正如在文档网站上提到的,<>和!=只是同义词。这意味着它们是完全可以互换的。php的历史有点复杂,所以命名约定,甚至到如何调用操作符,过去和现在都不是真正统一的。

#5


2  

According to PHP manual: http://fr.php.net/manual/en/language.operators.comparison.php it does not seem to have any difference.

根据PHP手册:http://fr.php.net/manual/en/language.operators.org/。PHP似乎没有任何区别。

#6


2  

There is no difference. I guess <> is something that was added in a later version of php. Kind of reminds me of Python. I think it the same with using AND or && for the and operator

没有区别。我猜<>是在后来的php版本中添加的。这让我想起了Python。我认为对于AND运算符的使用和&&也是一样的

#7


0  

It isn't any different, but I think I remember != was faster once, because I ran a test and found out <> was executing the "diff" methods of the objects I was comparing, which can be slower than the "compare" methods.

这没有什么不同,但是我记得!=的速度更快,因为我运行了一个测试,发现<>正在执行我正在比较的对象的“diff”方法,这比“compare”方法要慢。