3

I'm reading about php and it says,

== is Equality such that $a == $b is true if $a and $b have the same elements.

=== is Identity such that $a === $b is true if $a and $b have the same elements, with the same types, in the same order.

So, I thought I'd try and see the difference myself and wrote with this little script:

$a = array(1, 2, 3);
$b = array(2, 3, 1);
if ($a==$b) {echo "yeehaw!";} else {echo "nope";}
if ($a===$b) {echo "yup";} else {echo "nope";}

My thought was that the same order wasn't required for two arrays to be equal. However, when I ran this, I got "nope" and "nope".

What is the difference?

Community
  • 1
  • 1
Michael Pitluk
  • 77
  • 4
  • 13
  • possible duplicate of [php == vs === operator](http://stackoverflow.com/questions/589549/php-vs-operator) – hjpotter92 Jul 14 '13 at 09:10
  • 1
    @hjpotter92 not duplicate, he talking about array –  Jul 14 '13 at 09:11
  • @hjpotter92 I read the answer to the potential duplicate and it seems to be regarding scalar operators, I'm asking about array operators, and equality and identity WRT orderings within arrays. – Michael Pitluk Jul 14 '13 at 09:13
  • So you want to say that you were not able to understand the straight forward description in the PHP manual? http://php.net/language.operators.array Which kind of other conception did the docs cause to you? You didn't outline so far. What do you expect the arrays to be, show what they are, show that your expectation matches. These important parts are missing. – hakre Jul 14 '13 at 09:31
  • 1
    possible duplicate of [PHP - Check if two arrays are equal](http://stackoverflow.com/questions/5678959/php-check-if-two-arrays-are-equal) – 웃웃웃웃웃 Jul 14 '13 at 09:52
  • @Deepu it's related but not a duplicate. – Ja͢ck Jul 15 '13 at 00:12

6 Answers6

5

The documentation[PHP.net] says:

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

Since your two arrays are not in the same order1, they don't have the same key-value pairs.

var_dump($a);
array(3) { 
    [0]=> int(1) 
    [1]=> int(2) 
    [2]=> int(3) 
}
var_dump($b);
array(3) { 
    [0]=> int(2) 
    [1]=> int(3) 
    [2]=> int(1) 
}

1 With regards to their construction via array(), which will index the arguments starting with 0.

phant0m
  • 16,595
  • 5
  • 50
  • 82
5

The arrays you've provided have the same set of values, but different key-value-pairs.

Try the following use case instead (same key-value-pairs in different order):

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

... and the following use case (different data types):

$a = array(1, 2, 3);
$b = array('1', '2', '3');
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • @AlexShesterov Ran my test and it got the results I was looking for. Thanks for giving the example of different data types too! – Michael Pitluk Jul 14 '13 at 09:25
4

My thought was that the same order wasn't required for two arrays to be equal.

To make it clear what the documentation meant by same key/value pairs, let's take a look at the actual array contents:

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

Clearly, the pairs are different.

So what about that "same order"?

To illustrate that, let's create $b a little different:

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

The == equality will be satisfied because the pairs are now the same. However, because arrays in PHP are ordered maps, a difference in pair order causes the === equality to fail.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

Two arrays are considered identical === if:

  • number of elements is the same
  • all data types are the same
  • all elements are in the same order
  • each array has the same key-value pairs
Expedito
  • 7,771
  • 5
  • 30
  • 43
0

What is the difference?

The difference between two arrays can mean different things, so this question is normally best answered by using the kind of difference function for arrays that match your expectations.

In your case, equality is (probably) satisfied by the array_diff() function:

!array_diff($a, $b) && !array_diff($b, $a);

If you say no, that's not what I'm looking for, please see the duplicate question "PHP - Check if two arrays are equal" I also left an extended answer there that shows the other possible differences and how to test for those as you're concerned about comparing values and not element (which are key/value pairs).

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
-1

1st one fails because the elements are different. 2nd one fails because elements are different although type is same. (both should same)

Namal
  • 2,061
  • 3
  • 18
  • 26