警告:为 foreach() 提供的参数无效


Warning: Invalid argument supplied for foreach()

我正在尝试制作一个购物车,其中此产品()用于显示购物车中的内容,但在foreach语句中它显示警告:为foreach()提供的参数无效

function product()
{
  foreach(@$_SESSION as $name=>$value)
  {
    if($value>0)
    {
      if(substr($name,0,5) == 'cart_'){
        $id= substr($name,5,(strlen($name-5)));
        echo $id;
      }
    }
  }
}

$_SESSION 未设置,它为 null。

每次你有一个foreach时,它都嵌套在一个if中:

if (isset($anything) && is_array($anything)) {
  foreach(@$anything ...) {
  }
}