You are on page 1of 4

Comparison operators: When equals doesn't equal equals

http://www.phpwomen.org/forum/index.php?t=rview&goto=3454&th=225#msg_3454
This article provides a brief tutorial on a common topic of confusion amongst PHP developers - the seemingly similar operators; equals (==) and identical (===). During your PHP programming, you will have undoubtedly used the equals operator to perform value comparisons, usually to do conditional processing. For instance; <?php $myVar1 = 'cat'; // a string $myVar2 = 'dog'; // also a string $myVar3 = 'cat'; // again a string // $myVar1 == $myVar2 will evaluate to boolean false // $myVar1 == $myVar3 will evaluate to boolean true if ($myVar1 == $myVar2) { echo 'TRUE'; } else { echo 'FALSE'; } if ($myVar1 == $myVar3) { echo 'TRUE'; } else { echo 'FALSE'; } $myVar4 = '42'; // a string $myVar5 = 42; // an integer // $myVar4 == $myVar5 will evaluate to boolean true // because == does a value comparison only if ($myVar4 == $myVar5) { echo 'TRUE'; } else { echo 'FALSE'; } $myVar6 = 42.00; // a float $myVar7 = 42; // an integer // $myVar6 == $myVar7 will evaluate to boolean true // because == does a value comparison only

if ($myVar6 == $myVar6) { echo 'TRUE'; } else { echo 'FALSE'; } ?>

The identical operator goes one step further, and compares not just the value, but the type of the operands. Let's use the identical operator on the examples given above and see what happens; <?php $myVar1 = 'cat'; // a string $myVar2 = 'dog'; // also a string $myVar3 = 'cat'; // again a string // $myVar1 === $myVar2 will evaluate to boolean false // $myVar1 === $myVar3 will evaluate to boolean true as both type and value are identical if ($myVar1 === $myVar2) { echo 'TRUE'; } else { echo 'FALSE'; } if ($myVar1 === $myVar3) { echo 'TRUE'; } else { echo 'FALSE'; }

$myVar4 = '42'; // a string $myVar5 = 42; // an integer // $myVar4 === $myVar5 will evaluate to boolean false // because the types are different - one is a string, if ($myVar4 === $myVar5) { echo 'TRUE'; } else { echo 'FALSE'; } $myVar6 = 42.00; // a float the other an integer

$myVar7 = 42; // an integer // $myVar6 == $myVar7 will evaluate to boolean false // because the types are different - one is a float, if ($myVar6 === $myVar7) { echo 'TRUE'; } else { echo 'FALSE'; } ?> The identical operator is also useful for comparing arrays; <?php $myVar8 = Array('blue'); // an array with one element $myVar9 = 'blue'; // a string // $myVar8 === $myVar9 will evaluate to boolean false // because one is a string, and the other is an array of one element (even though the one element is a string) if ($myVar8 === $myVar9) { echo 'TRUE'; } else { echo 'FALSE'; } $myVar10 = Array(1,1,3,5,8,13,21,35); // an array of integers $myVar11 = Array(1.0, 1.0, 3.0, 5.0, 8.0, 13.0, 21.0, 35.0); // an array of f loats $myVar12 = Array('1', '1', '3', '5', '8', '13', '21', '35'); // an array of s trings // even though the variables above are all arrays, // they are arrays of different types of values (integer, // and are therefore not identical if ($myVar10 === $myVar11) { echo 'TRUE'; } else { echo 'FALSE'; } if ($myVar10 === $myVar12) { echo 'TRUE'; } else { echo 'FALSE';

the other an integer

float, string)

} if ($myVar11 === $myVar12) { echo 'TRUE'; } else { echo 'FALSE'; } ?> In many cases, the equals operator will be sufficient for the task at hand. However, there are many tasks for which the identical operator is superior;  When comparing variables that will be used to update a database (for instance, ensuring that an expected integer value is in fact an integer instead of a string designed to inject malicious SQL) When comparing arrays where elements of the same type (say floats, integers, strings etc) are assumed. Remember, assuming that values passed are those that are expected is a fundamental mistake made by many programmers. Comparing that resources (such as database connections, file handlers etc) are of the expected type.

More information on the equals and identical operator can be found at; h t t p : / / a u2 . p h p . n e t /o p e r a t o r s . c o m p a r i s o n

You might also like