67

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

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.

7
  • 3
    Well there is the fact that most people dont use <> as an op for non-equality in php even though its allowed :-) Commented Nov 27, 2010 at 23:54
  • 2
    != is probably more common... Commented Nov 27, 2010 at 23:55
  • @prodigitalson that may actually be and argument :) (readability etc) Commented Nov 27, 2010 at 23:57
  • @everybody, any difference in speed? (though probably no significant at all) Commented Nov 27, 2010 at 23:58
  • 2
    <> may be convenient for VBA, Pascal or Excel programmers Commented Jul 2, 2016 at 13:19

5 Answers 5

69

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

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

%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
Sign up to request clarification or add additional context in comments.

Comments

9

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

Comments

7

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

Comments

6

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.

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

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

Comments

3

The operators <> and != are the same.

However, as a matter of style, I prefer to use <> when dealing with numerical variables.

That is, if:

  • $a is an integer
  • $b is an integer

instead of asking:

// if $a is not equal to $b
if ($a != $b)

I will ask:

// if $a is either less than or greater than $b
if ($a <> $b)

This is a visual hint / reminder in my code that $a and $b are definitely both intended to be numerical rather than one or both being intentionally strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.