Equality and Identical Comparison Operators
November 21st, 2009Two types of comparison operators test for different levels of equality in PHP: the double equals and the triple equals sign. As I mentioned mentioned in my previous post, a single equals indicates a variable assignment and is not a comparison operation.
So, what’s the difference between the two?
- The equals comparison == tests for equality of values.
- The identical comparison as indicated by the triple equals === tests for equality of values and the type of those values. Both the values and type must be the same for the condition tested to be true.
Equals Example ==
The following example compares the equality of the number 57 and the string “057″. Although the values look different, they are same as leading zeros are discarded.
<?php
$anInteger = 57;
$aString = "057";
if ($anInteger == $aString) {
echo("This condition is true.\n");
}
else {
echo("This condition is false.\n");
}
?>
Program output: This condition is true.
Identical Comparison Example ===
Changing the if condition of the example above to
if ($anInteger === $aString) {
causes the types to be checked for the equality of not only their values but also their types. In this example, the values are the same; however types integer and string are different. The condition evaluates to false.
Program output:
This condition is false.
Another Equals Example ==
I intentionally mistype the name of the variable tested in the condition, transposing two characters. Oddly, the comparison evaluated below is always true.
<?php
$anInteger = 57;
if ($anItneger == 0) {
echo("This condition is true.\n");
}
else {
echo("This condition is false.\n");
}
?>
Program output: This condition is true.
Because I mistyped $anItneger, it is not initialized to a value. The lack of a value indicates that the value is null. Null is equivalent to the number 0, and the values tested in this comparison are same, evaluating to true.
The triple equals detects that the types of null and the number 0 are different, yielding a false result.
Different Types. Same Values.
In the list below all these values are the same. Testing equality for any of these values using an equals comparison == is always true. An identical comparison using the triple equals === is always false as their types are different.
The first group of values have the connotation of false.
| Type | Value |
|---|---|
| boolean | false |
| integer | 0 |
| float | 0.0 |
| null | |
| string | “” |
The second group of values below have the connotation of true.
| Type | Value |
|---|---|
| boolean | true |
| integer | 1 |
| float | 1.0 |
| class | instance of an object |
Object Equals Comparison ==
The behavior of the equals and identical comparisons behave differently when comparing two objects.
When value equality == compares two objects, the must be created from the same class. Furthermore, each property from both objects must have the same value.
Consider a PHP file that uses two chair objects. Here is the file for the chair objects.
File: Chair.php
<?php
class Chair {
private $hasBack;
private $legCount;
public function setLegCount($legCount) {
$this->legCount = $legCount;
}
public function setHasBack($hasBack) {
$this->hasBack = $hasBack;
}
}
?>
The chair has two properties: the number of legs the chair has and whether chair has a back.
In the example below two chairs are instantiated, both have four legs.
<?php
require_once('Chair.php');
$chair = new Chair();
$stool = new Chair();
$chair->setLegCount(4);
$stool->setLegCount(4);
if ($chair == $stool) {
echo("Condition is true.\n");
}
else {
echo("Condition is false.\n");
}
?>
The properties $legCount in both objects are set to 4. The second property, $hasBack, is uninitialized having no value or null. Both properties have the same values: 4 and null. The value of each property are the same between the two object, and the objects are initialiated from the class Chair. The value equality operator used on the two objects is true, giving the output of:
Condition is true.
This example sets an additional property with a different value in each object.
<?php
require_once('Chair.php');
$chair = new Chair();
$stool = new Chair();
$chair->setHasBack(true);
$chair->setLegCount(4);
$stool->setHasBack(false);
$stool->setLegCount(4);
if ($chair == $stool) {
echo("Condition is true.\n");
}
else {
echo("Condition is false.\n");
}
?>
Although the two object compared are instantiated from the same class and the number of legs property has the same value, the $hasBack property is true in $chair but false in $stool. Because of this difference in values, the equals comparison is false.
Program output: Condition is false.
Object Identical Comparison ===
PHP 5 object identical comparison differs from PHP 4. The PHP 5 identical operator tests if two variables reference, or point to, the same object. Diagrams are helpful in this situation.
In the following example, $chair and $stool have the same property values and are created from the same class.
<?php
require_once('Chair.php');
$chair = new Chair();
$stool = new Chair();
$chair->setLegCount(4);
$stool->setLegCount(4);
if ($chair == $stool) {
echo("Condition is true.\n");
}
else {
echo("Condition is false.\n");
}
?>
The each variable references, or points to, its own instance of a Chair class. Since the variables do not reference the same object (the same instance of a class), the identical comparison is false.
If I were to change the example to:
<?php $chair = new Chair(); $stool = new Chair(); $chair->setLegCount(4); $stool->setLegCount(4); $stool = $chair; ?>
The statement $stool = $chair; points $stool at the object that $chair is pointing at.
Since $stool and $chair point at the same object, the identical operator returns true.

