Invalid argument supplied for foreach()

后端 未结 19 1781
花落未央
花落未央 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:50

    foreach ($arr ?: [] as $elem) {
        // Do something
    }
    

    This doesen't check if it is an array, but skips the loop if the variable is null or an empty array.

提交回复
热议问题