0

I get a syntax error on the case "remove". I've been trying to fix it but I can't figure it out. Can anyone tell me how to fix it?Thanks

The code is for a online shop shopping cart

switch($action)
{
    case "add":
    if (isset($_SESSION['cart'][$id]))
        $_SESSION['cart'][$id]++;
    else
        $_SESSION['cart'][$id]=1;
break;

case "remove":
    if (isset($_SESSION['cart'][$id]))
    (
        $_SESSION['cart'][$id]--; (ERROR HERE)
        if ($_SESSION['cart'][$id]==0)
            unset($_SESSION['cart'][$id]);
    )
break;

case "empty":
    unset($_SESSION['cart']);
break;

}
cmario
  • 83
  • 2
  • 12

3 Answers3

6

You are using parenthesis instead of brackets for your IF statement:

if (isset($_SESSION['cart'][$id]))
( <-- HERE
    $_SESSION['cart'][$id]--; (ERROR HERE)
    if ($_SESSION['cart'][$id]==0)
        unset($_SESSION['cart'][$id]);
) <-- HERE
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

Please replace ( and ) with { and }

Corrected code:

case "remove":
    if (isset($_SESSION['cart'][$id]))
    {
        $_SESSION['cart'][$id]--; (ERROR HERE)
        if ($_SESSION['cart'][$id]==0)
            unset($_SESSION['cart'][$id]);
    }
break;
Barmar
  • 741,623
  • 53
  • 500
  • 612
Pupil
  • 23,834
  • 6
  • 44
  • 66
0

// Syntactical Errors, Find the corrected code!!

  switch($action)
  {
case "add":
    if (isset($_SESSION['cart'][$id])) {
        $_SESSION['cart'][$id]++;
    }
    else {
          $_SESSION['cart'][$id]=1;
     }
    break;

case "remove":
    if (isset($_SESSION['cart'][$id])) {
            $_SESSION['cart'][$id]--; //(ERROR HERE)
        }
        if ($_SESSION['cart'][$id]==0) {
            unset($_SESSION['cart'][$id]);
        }
    break;

case "empty":
    unset($_SESSION['cart']);
    break;

  }
Pankaj Garg
  • 1,272
  • 15
  • 21
  • 1
    Please don't promote writing `if` statements without braces around the body. – Barmar Apr 21 '14 at 17:41
  • @Barmar : Thanks for the tip! Can you please let me know what is the harm if i dont use bracket for single line statement after if ?? – Pankaj Garg Apr 21 '14 at 17:46
  • http://stackoverflow.com/questions/12193170/whats-the-purpose-of-using-for-a-single-line – Barmar Apr 21 '14 at 17:49