Posted by: phillipnb | July 10, 2010

Logical Errors with Identity and Equivalance Operators


There are several techniques and topics in every programming language which a developer may not understand to its core and depth. He or She may understand it on the surface but when it comes to implementing it in a piece of code, most often they make it wrong proving the fact that they did not understand it properly. One such thing in PHP is the difference between an identity operator (===) and the equivalence operator (==). In this article I’ll show you a few examples which will demonstrate the misuse of identity, equivalence, not equivalent and not identical operators.

The text book definition of an identity operator (===, three equal signs) is that this operator evaluates to true if and only if the operands on either side of this operator are of the same data type and have the same value. The definition for the equivalence operator (==, two equal signs) is that the two operands on either side of the operator are equivalent if they have the same value (they need not be of the same type and hence they can be converted to a common data type which may have the same value). Here is an example:


$myVar = '987x';
if ($myVar == 987)
{
echo "\n myVar has the value 987";
}
else
{
echo "\n myVar does not have the value 987";
}

If we run the above code, PHP will display the output as “myVar has the value 987”. You may be wondering how this happened?. When the above code is run, PHP converts the value of $myVar (which is ‘987x’) to an integer value of ‘987’ (remember that this is still a non integer datatype, only the value is converted to an integer). It then uses the equivalence operator to compare with the value on the right side of the equivalence operator in the ‘if-then-else’ statement.

Consider the same example (as shown above), except that this time we will use the identity operator instead of the equivalence operator.


$myVar = '987x';
if ($myVar === 987)
{
echo "\n myVar has the value 987";
}
else
{
echo "\n myVar does not have the value 987";
}

If we run this code, PHP will display the output as “myVar does not have the value 987”. You may be wondering why PHP is saying that $myVar does not have the value as 987. Remember the rule for an identity operator, which says that, both the value as well as the data type should be the same if the identity operator were to evaluate the operation as true. In this case, PHP did convert $myVar to 987 but it did not convert the data type to an integer. So, 987 is stored in a non integer data type and hence PHP gives the output as ‘myVar does not have the value 987’. To make PHP display the output for the above code as “myVar has the value 987”, you need to typecast $myVar to an ‘int’ data type. This can be seen in the example below:


$myVar = '987x';
$myVar = (int) $myVar;
if ($myVar === 987)
{
echo "\n myVar has the value 987";
}
else
{
echo "\n myVar does not have the value 987";
}

Let us now consider the negative cousins of both the identity and equivalence operators which are the ‘Not Identical Operator’ and the ‘Not Equivalent Operator’ respectively. The definition for a ‘Not Identical Operator’ is that an evaluation will be true if and only if two operands are not of the same data type or the operands do not have the same value. The definition for a ‘Not Equivalent Operator’ is that it will evaluate to true if the two operands are not equivalent irrespective of what data types the two operands were. Here are a few examples which will demonstrate the use of Not Identical and Not Equivalent Operators: Suppose a developer wrote a piece of code to check if the variable called $bankCode is present,


$bankCode = NULL;
if ($bankCode !== false)
{
echo "\n Bank Code is Present";
}
else
{
echo "\n Bank Code is Absent";
}

The output for the above code is ‘Bank Code is Present’ which is not what the developer had planned. What PHP is giving as output is correct because ‘NULL’ is not equal to ‘false’ both in terms of its data type as well as its value. If we had suppressed all warnings and error messages and forgot to declare $bankCode, still the output will be ‘Bank Code is Present’ – This is not what any developer would have planned with this piece of code. The developer wanted the code to display that the bank code is not present whenever the variable $bankCode is empty or null or even not present. To achieve this, we need to use the ‘Not Equivalent Operator’ instead of the ‘Not Identical Operator’.
Here is the revised code:


$bankCode = NULL;
if ($bankCode != false)
{
echo "\n Bank Code is Present";
}
else
{
echo "\n Bank Code is Absent";
}

If we run the above code, PHP will output the text ‘Bank Code is Absent’ whenever the variable $bankCode is NULL, empty or does not exist. This is exactly what every developer would want, the code should display the text ‘Bank Code is Present’ only if there is some value in the variable called $bankCode.

Let us move on and consider another example: Suppose a developer would like to check if a particular value is present in a variable called $bankCode and he writes the following code:


$bankCode = 'ABC200';
$bankCodePresent = stripos($bankCode,'ABC200');
if ($bankCodePresent != false)
{
echo "\n Bank Code is Valid";
}
else
{
echo "\n InValid Bank Code";
}

The output for the above should have been ‘Bank Code is valid’ but unfortunately PHP outputs the text ‘InValid Bank Code’. The function stripos($bankCode,’ABC200′) evaluates to ‘0’ which is basically false and when we compare ‘false’ != ‘false’ which is false, PHP will display the output as ‘InValid Bank Code’. To fix this problem, we need to use the ‘Not Identical Operator’ instead of ‘Not Equivalent Operator’. Here is the revised code:


$bankCode = 'ABC200';
$bankCodePresent = stripos($bankCode,'ABC200');
if ($bankCodePresent !== false)
{
echo "\n Bank Code is Valid";
}
else
{
echo "\n InValid Bank Code";
}

If we run the above code, if the stripos() evaluation is true, then the variable $bankCodePresent will have a value which is not identical with ‘false’ and hence PHP will display the text ‘Bank Code is Valid’.

The conclusion of this short story is that always be careful when you use the Identity and Equivalence operators. Understand thoroughly about how these operators function and have a clear idea of what you want to achieve using these two operators. Once you are clear on all these, the chances of writing bad code is almost zero.

Till Next time…

Happy PHPing

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: