Invalid argument supplied for foreach()

后端 未结 19 1405
花落未央
花落未央 2020-11-21 06:32

It often happens to me to handle data that can be either an array or a null variable and to feed some foreach with these data.

$values = get_val         


        
19条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 06:46

    Personally I find this to be the most clean - not sure if it's the most efficient, mind!

    if (is_array($values) || is_object($values))
    {
        foreach ($values as $value)
        {
            ...
        }
    }
    

    The reason for my preference is it doesn't allocate an empty array when you've got nothing to begin with anyway.

提交回复
热议问题