55

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?

6
  • I've never seen the <> operator. Commented Mar 12, 2012 at 17:25
  • 9
    @Rocket well now you have ^_^ Commented Mar 12, 2012 at 17:26
  • 2
    @Rocket <> is also used in sql for inequality Commented Mar 12, 2012 at 17:27
  • 1
    @StefanH: As well as != :-P Commented Mar 12, 2012 at 17:28
  • 1
    Of course, in a lot of cases, you're better off using !== anyway. Commented Dec 6, 2013 at 17:20

7 Answers 7

83

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

Sign up to request clarification or add additional context in comments.

Comments

29

No difference.

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

Comments

29

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.

Comments

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.

Comments

2

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

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.