TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-12-2007, 11:24 AM   #1 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default Identity and Equivalence

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)        // returns True

if (1.0 == 1)        // returns True

if ("1" == 1)        // returns True

if (NULL == 0)    // return True

if (== 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)        // returns True

if (1.0 === 1)        // returns False

if ("1" === 1)        // returns False

if (NULL === 0)    // return False

if (=== 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

Last edited by Karl : 09-12-2007 at 02:55 PM.
Karl is offline  
Reply With Quote
Old 09-13-2007, 12:40 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Informative article. I think most people will get confused based on the fact I get asked this question a lot. Especially on functions that are capable of returning a zero even though that is a positive return, such as the aforementioned strpos. It's something people should be consciously aware of when coding their scripts as otherwise it's just blind conformity.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:20 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design