How do I pass undefined vars to functions without E_NOTICE errors?

前端 未结 4 1777
深忆病人
深忆病人 2020-12-20 12:48

Pretty simple question really, how do I pass undefined vars to functions without E_NOTICE errors?

When passing undefined variables to functions such as isset(), no e

4条回答
  •  感动是毒
    2020-12-20 13:15

    Maybe something like this will work for you? It's a little bit hackish, but I think what you want is not quite possible without lazy evaluation of function arguments.

    if (atLeastOneIsSet($myArray, 1, 3, 4, 6)) doSomething();
    
    function atLeastOneIsSet(array $arr) {
        $args = func_get_args();
        for ($i = 1; $i < func_num_args(); ++$i)
            if (isset($arr[$args[$i]])) return true;
        return false;
    }
    

    Edit: oops, logic issues.

提交回复
热议问题