PHP Compare Objects

Summary: in this tutorial, you will learn how to compare objects in PHP using the comparison operator ( ==) and identity operator (===).

To compare objects in PHP, you can use either the comparison operator (==) or identity operator (===). However, each operator behaves differently based on two main criteria:

  • Objects are the same instance or different instances of a class
  • Object’s properties and their values.

It’s easier to understand how the comparison operator and identity operator work via an example.

Let’s create a new class called Point that has two properties x coordinate and y coordinate for the demonstration.

<?php

// Point.php

class Point
{
	private $x;

	private $y;

	public function __construct($x, $y)
	{
		$this->x = $x;
		$this->y = $y;
	}
}Code language: HTML, XML (xml)

Comparing objects using the comparison operator (==)

When you compare objects using the comparison operator (==), two objects are equal if they are instances of the same class and have the same properties and values.

First, create two new Point objects with the same properties’ values and compare them:

<?php

require 'Point.php';

$p1 = new Point(10, 20);
$p2 = new Point(10, 20);

if ($p1 == $p2) {
	echo 'p1 and p2 are equal.';
} else {
	echo 'p1 and p2 are not equal.';
}Code language: HTML, XML (xml)

It returns the following message:

p1 and p2 are equal

Second, assign  $p2 to a new reference $p3. In this case, both  $p2 and $p3 are referencing the same object.

<?php

require 'Point.php';

$p1 = new Point(10, 20);
$p2 = new Point(10, 20);

if ($p1 == $p2) {
	echo 'p1 and p2 are equal.';
} else {
	echo 'p1 and p2 are not equal.';
}

$p3 = $p2;
if ($p2 == $p3) {
	echo 'p2 and p3 are equal.';
} else {
	echo 'p2 and p3 are not equal.';
}
Code language: HTML, XML (xml)

In this example, $p2 and $p3 are also equal.

Third, create a new Point object with different properties’ values and compare it with $p3:

$p4 = new Point(20, 10);
if ($p3 == $p4) {
	echo 'p3 and p4 are equal.';
} else {
	echo 'p3 and p4 are not equal.';
}Code language: PHP (php)

In this example, $p3 and  $p4 are not equal.

Comparing objects using identity operator (===)

When you use the identity operator (===) to compare objects, they are identical if and only if both of them reference the same instance of a class.

The following  $p1 and  $p2 objects are identical when we use identity operator ( ===) to compare because they reference the same object.

<?php

require 'Point.php';

$p1 = new Point(10, 20);
$p2 = $p1;

if ($p1 === $p2) {
	echo 'p1 and p2 are identical.';
} else {
	echo 'p1 and p2 are not identical.';
}Code language: HTML, XML (xml)

However the following  $p3 object is not identical to the  $p1 even their properties values are the equal.

<?php

require 'Point.php';

$p1 = new Point(10, 20);
$p2 = $p1;

if ($p1 === $p2) {
	echo 'p1 and p2 are identical.';
} else {
	echo 'p1 and p2 are not identical.';
}

$p3 = new Point(10, 20);
if ($p1 === $p3) {
	echo 'p1 and p3 are identical.';
} else {
	echo 'p1 and p3 are not identical.';
}
Code language: HTML, XML (xml)

The following table illustrates the differences between == and === operators when comparing two objects:

Criteria=====
Two objects reference the same instancetruetrue
Objects with matching propertiestruefalse
Objects with different propertiesfalsefalse

Summary

  • The comparison operator (==) returns true if two objects are the same or different instances of a class with the same properties’ values.
  • The identity operator (===) returns true if two objects reference the same instance of a class.
Did you find this tutorial useful?