A lot of people don’t seem to know the real difference between the
equivalence and
identity operators, this short articles aims to change that.
Equivalence Operator
The most common comparison operator has got to be the equivalence operator (==), which, as the name suggest, compares one operand against another to see if they’re equal. If the two operands’ values are loosely equal (by that I mean the values are the same, but the data types needn't be the same) then the test evaluates to true. Let’s take a look at some examples:
PHP Code:
if (1 == 1) // returns True
if (1.0 == 1) // returns True
if ("1" == 1) // returns True
if (NULL == 0) // return True
if (2 == 1) // return False
As you can see above, the equivalence operator actually converts both operands to a common data type and then performs the comparison. This allows you to compare equal values of different data types, but you must be careful, this power can leave you in trouble.
For example,
strpos returns the index of the first occurrence of a needle within a haystack (basically it returns the index of one string within another), however if the needle cannot be found the functions returns FALSE.
NULL, as you may or may not know is equal to 0 when using the equality comparison operator. So, let’s consider the following example:
PHP Code:
$szWord = 'Hello';
$iPosition = strpos($szWord, 'H');
if ($iPosition == FALSE)
{
die("Could not find H in $szWord");
}
die ("Found H at index $iPosition");
If you where to run this small example you would see that the script returns "Could not find H in Hello", even though we know that H is there. The reason behind this is because H is at index 0 of the string $szWord, and since 0 and NULL are both equal when converted to a common data type (using the equivalence operator) the comparison always returns true.
In order to overcome this problem we will need to evaluate the two operands using the identity operator (===).
The Identity Operator
The identity operator uses three equal signs and is used to determine if one value is identical to another. This comparison only evaluates to true if the two values are of the same value and data type, rather than the equivalence operator which works solely on the value of the operand.
Let’s see some examples of this:
PHP Code:
if (1 === 1) // returns True
if (1.0 === 1) // returns False
if ("1" === 1) // returns False
if (NULL === 0) // return False
if (2 === 1) // return False
If you compare these with the first examples I gave you will see that comparisons such as (1.0 === 1) will now return False. If you haven’t grasped this yet, the reason behind this is because 1.0 is a floating point value and 1 is an integer, thus their data types are not equal (even though their values are).
Finally let’s amend our previous
strpos example, this time we’ll use the identify operator to compare the result of
strpos.
PHP Code:
$szWord = 'Hello';
$iPosition = strpos($szWord, 'H');
if ($iPosition === FALSE)
{
die("Could not find H in $szWord");
}
die ("Found H at index $iPosition");
Notice that the only change is the comparison operator (== is now === - equivalence is now identity), this one small change now handles FALSE and 0 as different values (rather than converting them to a common data type), thus this example will no longer fail on this condition. If you now go ahead and run the amended example you will see that it now returns "
Found H at index 0", which is the expected result.
Conclusion
I hope this article has helped shine some light onto these easy, but powerful comparison operators. It’s important to remember the differences as it can get you in a world of trouble if you don’t remember NULL == 0 evaluates to true, especially when using functions that can return both 0 and FALSE, such as
strpos. To conclude, always remember that for any two expressions,
$a == $b is TRUE if $a is equal to $b.
$a === $b is TRUE if $a is equal to $b, and they are of the same type.
THanks to Salathe for pointing out some stuipid mistakes