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.
==' operator checks the unequality of two objects with a type check. It does not converts the datatype and makes a typed check. For example 1 !== '1' will results true.
The spaceship operator <=> is the latest comparison operator added in PHP 7. It is a non-associative binary operator with the same precedence as equality operators ( == , !=
Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result. Here is the follow up question I received right I answer that there is no difference between those operator.
!= will only check value regardless of operands type. but !== is used to compare both value & type of 2 operands that are being compared to each other.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With