Difference between equality(==) and identity(===) in PHP

Double equals(==) compares the values of variables for equality, type casting as necessary. Triple equals(===) checks if 2 variables are of the same TYPE and have the same value.
A full explanation of the differences are available in PHP Manual
1 === 1: true
1 == 1: true
1 === “1”: false //1 is an integer, “1” is a string
1 == “1”: true //“1” gets casted to an integer, which is 1
“foo” === “foo”: true //both operands are strings and have the same value
Note: two instances of the same class do NOT match the === operator
$a = new stdClass();
$a->foo = “bar”;
$b = clone $a;
var_dump($a === $b); //bool(false)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s