PHP – This is not a bug, it’s a feature
11 Jun 2014
Type-juggling can go so wrong. Try this:
echo in_array(0, array('foobar')) ? 'true' : 'false' ;
It will output true, which seems so very very wrong. PHP indeed tries to convert ‘foobar’ to a number, and because that is not really possible, it becomes 0. So in_array
returns true.
$array = array('1','2'); echo in_array( 0, $array ) ? 'true ' : 'false ' ; // false echo in_array( '0', $array ) ? 'true ' : 'false ' ; // false $array = array('this is a string); echo in_array( 0, $array ) ? 'true ' : 'false ' ; // true echo in_array( '0', $array ) ? 'true ' : 'false ' ; // false $array = array(1,2,'string'); echo in_array( 0, $array ) ? 'true ' : 'false ' ; // true echo in_array( '0', $array ) ? 'true ' : 'false ' ; // false