-2

how to add $_SESSION[] in function

function get_cart_list()
{
    global $_SESSION['items'];
    if (isset($_SESSION['items'])) 
    {
        foreach ($_SESSION['items'] as $key => $val)
        {
            $gete = $key;
            $total = 0;
            include ('gc-cache-product.php');
            $jumlah_harga = ( $skupricenew ) * $val;
            $total += $jumlah_harga;
            include ($themeid."/content/part/cart/cart_list_item.php");
        }
    }
}

syntax error, unexpected '[', expecting ',' or ';' in /home/clients/xxx/html/website/template/shadows/gc-cart.php on line 2

thank you

Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35
grenz
  • 47
  • 8

1 Answers1

4

First, $_SESSION is a superglobal. It's already global; you don't need to explicitly say you're using it as a global. From the manual:

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

Second, you can't use an array element with global. It's just global $foo;, not global $foo['bar'];. That's why you get a syntax error.

In the future, when you get errors like this, you may want to refer to this reference question and its answers.

elixenide
  • 44,308
  • 16
  • 74
  • 100