Invalid argument supplied for foreach()

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

    I'll use a combination of empty, isset and is_array as

    $array = ['dog', 'cat', 'lion'];
    
    if (!empty($array) && isset($array) && is_array($array) {
        //loop
        foreach ($array as $values) {
            echo $values; 
        }
    }
    

提交回复
热议问题