php array_search 0 index
This is explicitly mentioned in the docs. You need to use ===
or !==
:
$key = array_search(...);
if ($key !== false) ...
Otherwise, when $key
is 0
, which evaluates to false
when tested as a boolean.
The conditional in your second example block gives execution order priority to the !==
operator, you want to do the opposite though.
if (($key = array_search(100,$test)) !== false) {
!==
has higher precedence than ==
which makes the parentheses necessary.