-2

I'm trying to make a shoping cart in which this product() is used for displaying what is in cart but in foreach statement it shows Warning: Invalid argument supplied for 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;
      }
    }
  }
}
wogsland
  • 9,106
  • 19
  • 57
  • 93
  • 3
    start by removing the @ (it's an error suppressor), check for errors and make sure you started the session – Funk Forty Niner Jan 30 '16 at 21:46
  • @ ?? as @fred-ii mentioned chk r u using session_start(); ??? At least chk var_dump($_SESSION); – devpro Jan 30 '16 at 21:48
  • Hi @fred-ii I am searching how can I debug the error in my code on SO :) LINE BY LINE.. – devpro Jan 30 '16 at 21:53
  • `$_SESSION` is null, you should first check if is an array before loop it! `is_array` http://php.net/manual/en/function.is-array.php – Oden Feb 27 '16 at 10:50

1 Answers1

0

$_SESSION is not set, it's null.

EVERY time you have a foreach nest it in an if:

if (isset($anything) && is_array($anything)) {
  foreach(@$anything ...) {
  }
}
Gavriel
  • 18,880
  • 12
  • 68
  • 105