I've always done this: if ($foo !== $bar)
But I realized that if ($foo != $bar)
is correct too.
Double =
still works and has always worked for me, but whenever I search PHP operators I find no information on double =
, so I assume I've always have done this wrong, but it works anyway. Should I change all my !==
to !=
just for the sake of it?
!== should match the value and data type
!= just match the value ignoring the data type
$num = '1';
$num2 = 1;
$num == $num2; // returns true
$num === $num2; // returns false because $num is a string and $num2 is an integer
$a !== $b
TRUE if $a
is not equal to $b
, or they are not of the same type
Please Refer to http://php.net/manual/en/language.operators.comparison.php
You can find the info here: http://www.php.net/manual/en/language.operators.comparison.php
It's scarce because it wasn't added until PHP4. What you have is fine though, if you know there may be a type difference then it's a much better comparison, since it's testing value and type in the comparison, not just value.
Source: Stackoverflow.com