0

I have this code:

if($key == 'b306') {
    $fieldType = 'date';
  }

In my PHP Storm debugger, it says that key = 0 (integer). When I leave it as a ==, the code does pass into the if. Only if I change it to === does it skip it, as expected. Why? I thought that the difference between == and === is that === matches type and value, vs == only does value. But this shouldn't allow 0 to be equal to 'b306'.

user1015214
  • 2,733
  • 10
  • 36
  • 66
  • link to exact same[issue](http://stackoverflow.com/questions/589549/php-vs-operator) with answer – jcjunction Jul 10 '14 at 20:52
  • 1
    `echo (int)"b306";` will print zero. Also `echo intval("b306");`. So `"b306" == 0` is true. You could have simply googled this. – nl-x Jul 10 '14 at 20:53
  • When you use `==` with different expressions, one will get converted to fit (in this case the string, to 0). They won't with `===`, which explains the difference. – Max Jul 10 '14 at 20:55
  • So, why will "b306" be converted to an integer, aka 0, vs 0 be converted to a string, aka: "0"? Is it based on the order in the conditional? – user1015214 Jul 10 '14 at 21:06

0 Answers0