0

Check this out:

print_r( (int) in_array('hello',array( 0 => 0)) );

It returns 1

Since you probably will have trouble believing it, here is a codepad: http://codepad.org/XlNKbrFk

Here is the official reference:
http://php.net/manual/en/function.in-array.php

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Yes, it seems that is_array thinks that a random string and 0 are the same thing.

So my question is, did I find a bug? Is this behavior justified? Is that drunken logic or what? Can anyone help me? I am struggling with that part.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Rolf
  • 5,550
  • 5
  • 41
  • 61
  • Since you looked at the manual; the third parameter copes with that type juggling specifically. – mario Jan 13 '14 at 01:12

1 Answers1

1

It's not a bug. From the bug tracker

Not a bug... PHP indeed tries to convert 'foobar' to a number, and because that is not really possible, it becomes 0. That's why there is that third parameter too.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • OK, so PHP converts "foobar" to 0? Because if the array contains 0=>1, it will return false. – Rolf Jan 13 '14 at 01:45
  • It's not just `foobar`, it's any string value. It's typecasting it to an `(int)` from a string, which is why they made the quote above. – Machavity Jan 13 '14 at 01:50