views:

708

answers:

2

Hi :)

I have multiple switch statements in on one of the pages in order to pass different vsriables to the URL, as well as different case. I need these different switch statements because I need the different variables.

However, when I put a "default" in one of the switch statements, that default applies to every other switch statement and so when I use the variable of another switch statement in the URL, the default case of that other switch statement will appear on screen, along with the case of this switch statement.

All of my switch statements have one or more cases and I really cannot figure out how to get around this. Please may somebody help me?

Thanks in advance, Calum.

+3  A: 

This might be way off, but I think you need something like this:

if (isset($_POST['myvar'])) {
   switch ($_POST['myvar'] {
      case 1:
      ....
      break;
      default:
      ....
      break;
   }
} else if (isset($_POST['myvar2'])) {
   switch ($_POST['myvar2'] {
      case 1:
      ....
      break;
      default:
      ....
      break;
   }
}

Does that make sense?

mabwi
Thank you, I will try this and see if it works :)
A: 

make sure that you have a "break;" statement at the end of each of your cases, and that the default case is the last one. like this:

switch ($var) {
    case 1: // do stuff 1;
            break;
    case 3: // do stuff 2;
            break;
    // ...
    default: // do default stuff
}
farzad
You can also place default as the first item, it has no bearing on the outcome of the results of the statement.
Syntax
Thank you both for your comments and answers. I will also try this :)