-1

In the below code, the IF case is TRUE also for each time the $value is 0. To fix that, I had to change "==" to "===".

Can I please get an explanation why ? How come comparing to the specific string getting TRUE on 0 as well.

<?php

date_default_timezone_set('EST'); //time set to GMT base

$json_file = '[
  {
    "ACCOUNT_ID": -1,
    "ACC_NAME": "acc name not found",
    "TT4": 1955801736,
    "TT7": 0,
    "TT8": 0
  },
  {
    "ACCOUNT_ID": 841,
    "ACC_NAME": "Appnexus",
    "WEEK_REQ": 66306441448,
    "TT16": 16570453480,
    "TT_NULL": 0,
    "ORDER_ID": 1
  },
  {
    "ACCOUNT_ID": 730,
    "ACC_NAME": "MediaMath",
    "WEEK_REQ": 17816846018,
    "WEEK_CLASS_RATE": 87.65,
    "TT16": 0,
    "TT_NULL": 0,
    "ORDER_ID": 1
  }
]';



    $j = json_decode($json_file);

    foreach ($j as $row)
    {
        foreach($row as $key=>$value)
        {
            if ($value == 'acc name not found')
            {
                echo $key . " - " . $value;
                echo "<br>";
            }
        }       
    }

?>
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Nir B
  • 1
  • 3
    possible duplicate of [How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?](http://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp) – Rene Korss Aug 06 '15 at 06:35

1 Answers1

0

When you are comparing integer (or integer-like string) with string using == operator then, because of type juggling, string is converted to integer. Every string that not have number at it's beggining will become 0 (that is how converting string to int works). So when you are comparing 0 == 'acc name not found' you are in fact comparing 0 == 0

arbogastes
  • 1,308
  • 9
  • 10