Home » Php » php – What is the difference between <> and !=

php – What is the difference between <> and !=

Posted by: admin April 23, 2020 Leave a comment


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

if( A != B ) {

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

if( A <> B ) {

Is there any difference?

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

How to&Answers:

Forgetting documentation for a minute, let’s check out the source code. Let’s start with the scanner (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

Now, let’s check out the operation:


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

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


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.

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


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


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 !=.

They have the same order of precedence.


No difference.

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


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.


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


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


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.