1

I have a strange behaviour in my code.

function what_ever($array){
  foreach ($array as $key => $value){
    if($value === TRUE){
      switch($key){
        case 'xx':
          ....
      }
    }
  }
}

After the if the $key is empty/gone what ever. Code like this works fine:

function what_ever($array){
  foreach ($array as $key => $value){
    if($value == TRUE){
      switch($key){
        case 'xx':
          ....
      }
    }
  }
}

The second snippet is using only == instead of ===. But shouldn't it work anyway? If not why not?

DBR
  • 146
  • 1
  • 10
  • 2
    probably $value is evaluated to true but it's not "true" as value (i mean that it's not a boolean, but is something evaluated as true if converted to a boolean) and so the problem is not the "$key" that is disappearing, but the fact that you are not going inside the if – Alberto Sinigaglia Feb 23 '20 at 01:25
  • 1
    Take a read here on comparions operators https://www.php.net/manual/en/language.operators.comparison.php – Lounis Feb 23 '20 at 01:25
  • @Berto99 You are absolutly right. It's to late in germany, need to finish for today. Thanks for hitting my face into that. – DBR Feb 23 '20 at 01:29
  • well, i'm from italy and so i can understand you ahah – Alberto Sinigaglia Feb 23 '20 at 01:30
  • nevermind, it happens to all of us :) – Your Common Sense May 31 '20 at 15:36

0 Answers0