Comparing Arrays And Object

<?php

    $a = array('green', 4 => '3', 'c' => 'yellow');
    $b = array('green', 'c' => 'yellow', '4' => 0x3);
    $c = array('green', 'c' => 'yellow', '4' => '3');

    var_dump($a == $b);  // true   identical, whatever the order
    var_dump($a === $b); // false  identical, but not the order

    var_dump($c == $b);  // true   identical, with some type juggling
    var_dump($c === $b); // false  identical, but not at the type level

?>

== and === apply different algorithms to compare arrays.

== compares keys without taking order in account, while === also takes into account the order.

== applies type juggling to values, and then compare them loosely, while === makes a identity comparison, with value and type. == and === compare keys the same way, as they can only be int or string, and no type-juggling is applied.

The same rules apply when comparing objects: the order of assignations of the properties is used by == but not by ===.

Finally, comparing an array and an object always fails: one of them has to be cast.

See Also

PHP Features

Last updated: 14 July 2026