lupu51nfactum N778
lupu51nfactum N778

Reputation: 229

PHP - array comparison == and ===

Can someone clarify please why is the result of

$a = array (0 => 1, 1 => 2, 2 => 3);
$b = array (1 => 2, 2 => 3, 0 => 1); 
var_dump ($a === $b);

Boolean false and not Boolean true?

Upvotes: 0

Views: 59

Answers (6)

Ipsita Rout
Ipsita Rout

Reputation: 5179

$a === $b will be TRUE only if $a and $b have the same key/value pairs in the same order and of the same types.

Upvotes: 0

Blueblazer172
Blueblazer172

Reputation: 600

$a = array (0 => 1, 1 => 2, 2 => 3);
$b = array (1 => 2, 2 => 3, 0 => 1); 
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)

== compares the variable from $a with $b
=== compares the same datatypes from $a with $b

Upvotes: 0

Brijal Savaliya
Brijal Savaliya

Reputation: 1091

$a == $b Equal TRUE if $a is equal to $b, except for (True == -1) which is still True.

$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.

e.g.

"1" == 1; True "1" === 1; False

Upvotes: 0

Blinkydamo
Blinkydamo

Reputation: 1584

$a = array (0 => 1, 1 => 2, 2 => 3);
$b = array (1 => 2, 2 => 3, 0 => 1); 
$c = array (0 => 1, 1 => 2, 2 => 3);
var_dump( $a === $b );  // False
var_dump( $a === $c );  // True

The arrays must match, same order.

Upvotes: 0

Waleed Ahmed Haris
Waleed Ahmed Haris

Reputation: 1244

Double equals compare only values, and triple equals compare datatype also

Upvotes: 0

Mihai Matei
Mihai Matei

Reputation: 24276

As you can see in the PHP manual

$a === $b: TRUE if $a and $b have the same key/value pairs in the same order and of the same types.

Your arrays key/value pairs are not set in the same order so the result will be false

Upvotes: 3

Related Questions