Object comparison
The PHP language provides several comparison operators that allow us to compare two different values, resulting in either true
or false
:
==
: equal===
: identical!=
: not equal<>
: not equal!==
: not identical<
: less than>
: greater than<=
: less than or equal to>=
: greater than or equal to
While all of these operators are equally important, let's take a closer look at the behavior of the equal (==
) and identical (===
) operators in the context of objects.
Let's take a look at the following example:
<?php class User { public $name = 'N/A'; public $age = 0; } $user = new User(); $employee = new User(); var_dump($user == $employee); // true var_dump($user === $employee); // false
Here, we have a simple User
class with two properties set to some default values. We then have two different instances of the same class, $user
and $employee
. Given that both objects have the same properties, with the same values, the equal (==
) operator returns true
. The identical...