0

For this code:

<?php

$data = array('0' => 'A_VALUE');

foreach ($data as $key => $value) {
  if ($key == "Something to match the key on") {
    print_r($key);
    print_r('key matches');
  }
}

I get this output

0key matches

Online example here: http://3v4l.org/dKOWq#v431

I cannot for the life of me work out why I am getting this. The comparison should clearly return FALSE.

Can someone explain this to me?

DanSingerman
  • 36,066
  • 13
  • 81
  • 92
  • 2
    Use `===` and you are good. – davidkonrad Sep 10 '13 at 11:59
  • 2
    http://stackoverflow.com/questions/6843030/why-does-php-consider-0-to-be-equal-to-a-string – Bora Sep 10 '13 at 12:00
  • +1 to @davidkonrad. It's generally best to use `===` in preference to `==`; you'll save yourself a lot of this type of issue. – Spudley Sep 10 '13 at 12:02
  • btw, the same applies in Javascript, and other loose-typed languages. – Spudley Sep 10 '13 at 12:04
  • 2
    +1 to @Bora. Yet another question to spawn a dozen answers. – Your Common Sense Sep 10 '13 at 12:05
  • @bora - it is not a duplicate. This is because the foreach loop is casting the '0' to an int. That is an extra issue not in the other qusetion. – DanSingerman Sep 10 '13 at 12:09
  • 2
    This is a good question. If it is a duplicate, close it as such. It does not deserve down votes. – Jason McCreary Sep 10 '13 at 12:10
  • It doesnt matter. Try with `key($data)`, you'll get same result. This issue about wrong comparison and you can find the answer searching on SO and I can find dozen answers for you about your issue http://stackoverflow.com/questions/7205238/foreach-loop-issues-in-php etc.. – Bora Sep 10 '13 at 12:15
  • 1
    @bora - I am not claiming it is not a duplicate. I just think the duplicate it is linked to doesn't contain the full answer. The implicit casting of the key is a crucial ingredient. – DanSingerman Sep 10 '13 at 12:24

5 Answers5

7

Can someone explain this to me?

Type juggling.

Since you are using ==, PHP compares a 0 (int) to Something to match the key on (string). Both sides are cast to an int. So Something to match the key on become 0. And 0 == 0 is true.

Use strict equality or explicit casting.

For example:

if ((string)$key == (string)"Something to match the key on") {
  // code
}
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • This is correct. The subtlety I was missing was that the foreach loop is silently converting the '0' to an int. Could you please add that detail to your answer so that SO has the complete answer (note this detail makes it a different question to http://stackoverflow.com/questions/6843030/why-does-php-consider-0-to-be-equal-to-a-string) – DanSingerman Sep 10 '13 at 12:04
  • Indeed, type juggling is one of those *helpful* things in PHP. But sometimes it can be *too* helpful. I make it a habit to use strict equality using explicit casting if needed. – Jason McCreary Sep 10 '13 at 12:12
1

Use === to check for type AND value equality, to avoid confusion

cusimar9
  • 5,185
  • 4
  • 24
  • 30
1

When in doubt, check the type comparison tables. This page explains how PHP computes every possible type of comparisons.

Kethryweryn
  • 639
  • 4
  • 11
0

"===" is used for check with "Datatype"

used this code for compare

<?php

$data = array('0' => 'A_VALUE');

foreach ($data as $key => $value) {
  if ($key === "Something to match the key on") {
    print_r($key);
    print_r('key matches');
  }
} 
Shakti Patel
  • 3,762
  • 4
  • 22
  • 29
0

Always try to use === for equality this always gives you the correct result.

Manish
  • 193
  • 4