In the below in_array(0, $array)
evaluates to true
. That surprised me. I expected false
.
I know adding the third parameter to indicate a strict check like in_array(0, $array, true)
will give me the desired result.
That said, I'm very curious why in_array(0, $array)
evaluates to true
. At first I thought it was just seeing a string at the 0 index, but in_array(1, $array)
evaluates to false
so that doesnt seem to explain it.
Example: Runable fiddle here
<?php
$array = [
'inputs',
'app',
'post',
'server',
];
echo in_array('inputs', $array) ? '"inputs" is in the array...' : '';
echo "\n";
echo in_array(0, $array) ? '0 is also in the array!' : '';
echo "\n";
echo in_array(1, $array) ? '1 is also in the array!' : '1 is NOT in the array!';
Output:
"inputs" is in the array...
0 is also in the array! <---- huh?
1 is NOT in the array!