DaedalusAlpha
DaedalusAlpha

Reputation: 1727

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 is_int not to execute if array_item does not exist in $body. However I still get complaints from PHP about "undefined index array_item", which I assume is from $body['array_item'].

Can someone explain to me why $body['array_item'] is executed if is_int isn't?

Upvotes: 0

Views: 656

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

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();

Upvotes: 5

Related Questions