52
votes

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

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
I've never seen the <> operator.Rocket Hazmat
@Rocket well now you have ^_^Naftali aka Neal
After scouring the PHP docs, it seems both != and <> do "type juggling". So, they seem the same. php.net/manual/en/language.operators.comparison.phpRocket Hazmat
@Rocket <> is also used in sql for inequalityStefan H
@StefanH: As well as != :-PRocket Hazmat

7 Answers

80
votes

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:

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.

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

28
votes

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.

27
votes

No difference.

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

5
votes

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.

2
votes

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

2
votes

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

0
votes

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.