Current Location: Home> Latest Articles> How to Compare Objects in PHP: A Detailed Guide to Different Comparison Operators

How to Compare Objects in PHP: A Detailed Guide to Different Comparison Operators

gitbox 2025-06-17

1. Introduction

In PHP application development, comparing objects is a common operation, such as checking if two objects are equal. This article will explore how to compare objects in PHP and provide an in-depth analysis of the various comparison operators used for this purpose.

2. Comparing Objects in PHP

PHP provides several operators for comparing objects, such as ==, !=, ===, and !==, which are used to check if the objects have the same property values, if they are the same object, or if they have different property values.

The common comparison operators include:

  • ==

    In the example above, since $obj1 and $obj2 have the same property values, it outputs "The two objects are equal".

    2.2 != Operator

    The != operator is the opposite of ==. It checks if two objects have different property values. It returns true if the property values are different, otherwise it returns false.

    
    $obj1 = new stdClass();
    $obj2 = new stdClass();
    $obj1->name = '张三';
    $obj1->age = 20;
    $obj2->name = '李四';
    $obj2->age = 30;
    if ($obj1 != $obj2) {
        echo 'The two objects have different property values';
    } else {
        echo 'The two objects have the same property values';
    }
    

    In this example, since $obj1 and $obj2 have different property values, it outputs "The two objects have different property values".

    2.3 === Operator

    The === operator checks if two objects are the same object, meaning they point to the same memory address. It returns true if they are the same object, otherwise it returns false.

    
    $obj1 = new stdClass();
    $obj2 = $obj1;
    if ($obj1 === $obj2) {
        echo 'The two objects are the same object';
    } else {
        echo 'The two objects are not the same object';
    }
    

    In this example, since $obj2 points to the same memory address as $obj1, it outputs "The two objects are the same object".

    2.4 !== Operator

    The !== operator is the opposite of ===. It checks if two objects are not the same object, meaning they do not point to the same memory address. It returns true if they are not the same object, otherwise it returns false.

    
    $obj1 = new stdClass();
    $obj2 = $obj1;
    $obj3 = new stdClass();
    if ($obj1 !== $obj3) {
        echo 'The two objects are not the same object';
    } else {
        echo 'The two objects are the same object';
    }
    

    In this example, since $obj1 and $obj3 point to different memory addresses, it outputs "The two objects are not the same object".

    3. The Principle of Object Comparison

    When PHP compares objects using comparison operators, it automatically calls the __toString() method of the object, converting the object to a string for comparison. If the object does not implement the __toString() method, PHP will throw a "Catchable fatal error".

    Therefore, when comparing objects, it is recommended to ensure that both objects implement the __toString() method, to avoid errors. Also, PHP compares the property values of the objects, not the methods. In other words, if two objects have the same property values but different methods, PHP will still consider them equal.

    4. Conclusion

    This article introduced how to compare objects in PHP, including using different comparison operators. Developers should focus on the property values of objects during comparison and ensure that the objects implement the __toString() method to avoid errors.