PHP短路评估

Dae*_*pha 0 php operators short-circuiting

我有这个代码:

if (!in_array('array_item', $body) || !is_int($body['array_item']))
    throw new Exception();
Run Code Online (Sandbox Code Playgroud)

现在由于短路is_int,如果array_item不存在,我希望不会执行$body.但是我仍然从PHP那里得到关于"undefined index array_item"的抱怨,我假设它来自$body['array_item'].

有人可以向我解释为什么$body['array_item']不执行is_int

Ola*_*che 5

in_array在数组中查找.如果您想查看,如果存在密钥,请array_key_exists改用

if (!array_key_exists('array_item', $body) || !is_int($body['array_item']))
    throw new Exception();
Run Code Online (Sandbox Code Playgroud)