DaedalusAlpha DaedalusAlpha - 1 year ago 126
PHP Question

PHP short-circuit evaluation

I have this code:

if (!in_array('array_item', $body) || !is_int($body['array_item']))
throw new Exception();

Now due to short-circuiting I would expect
not to execute if
does not exist in
. However I still get complaints from PHP about "undefined index array_item", which I assume is from

Can someone explain to me why
is executed if

Answer Source

in_array looks for a value in an array. If you want to see, if a key exists, use array_key_exists instead

if (!array_key_exists('array_item', $body) || !is_int($body['array_item']))
    throw new Exception();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download