Invalid argument supplied for foreach()

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

    What about defining an empty array as fallback if get_value() is empty?
    I can't think of a shortest way.

    $values = get_values() ?: [];
    
    foreach ($values as $value){
      ...
    }
    

提交回复
热议问题