<?
$a = array(1,2,3);
$b = array(2,3);
var_dump($a==$b); // false
var_dump($a===$b); // false
array_shift($a); // $a = (2,3)
var_dump($a==$b); // true
var_dump($a===$b); // true (!)
$x = new StdClass();
$y = new StdClass();
var_dump($x == $y); // true
var_dump($x === $y); // false
class X {};
$p = new X();
$q = new X();
var_dump($p == $q); // true
var_dump($p === $q); // false
$r = array();
var_dump($p == $r); // false
var_dump($p === $r); // false
var_dump("X" == X); // true
var_dump("X" === X); // true
Tuesday, February 16, 2010
All animals are ==, but some animals are === than others.
Equality comparators work differently when it comes to types. Arrays of different instances are always equal (see previous post), objects of different instances are ==-equal but not ===-equal. Funnily, comparing classes (or functions) to strings turns out to be equal again (because identifiers are nothing more than strings ).
Subscribe to:
Posts (Atom)