I am trying to compare two values in PHP using the following piece of codes :
if ($val1 != $val2) .....
if ($val1 <> $val2) .......
Are both the above comparison code same in all aspects ??
I am confused which operator to use amongst these two ( != and <> ) !!
update :
Has this got something to do with comparing strings or numerical strings ??
The php documentation on Comparison Operators tells us that !=
and <>
in fact do the same.
Both are named “Not Equal”, and are described as:
TRUE if $a is not equal to $b after type juggling.
Most people use !=
though, as that is more conforming to other programming languages which do/may not support <>
. In fact, I don’t remember seeing <>
being used in any PHP code yet.
There is no difference, both have same meaning.
Please check this : http://php.net/manual/en/language.operators.comparison.php
I propose to use C-style comparison operator !=
Hope this helps.
Both do the same (see docs), but you should definitely use !=
. I have never seen anyone use <>
, ever.
I could imagine that this was taken from SQL, where <>
is used as the inequality operator, too.
i think the two are just the same
according to the php manual there is no difference
$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 think the biggest difference is in style and convention.
Most of the time I use != for comparisons. It just seems to read better because, to me at least, ! always means not. On the other hand when I want to do a comparison where I think about the comparison as 'less than' or 'greater than' but not 'equal to' I use <> (very rare).