Alko
Alko

Reputation: 1439

Php traverse through the switch, and pass through

is it possible in php to do something like this:

switch ($option) {

    case "a":
      if($val == 7) {
          include("page.php");
      } else {
          "go to default case";
      }
    break;

    case "b":
      include("somefile.php");
    break;

    default:
     include("404.php");
    break;

}

In the above case "a" if $val == 7 include page.php otherwize go to default switch case

Upvotes: 0

Views: 77

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Kind of a hackish way (I would look for a better way) but it would look like this:

switch ($option) {

    case "b":
      include("somefile.php");
    break;

    case "a":
      if($val == 7) {
          include("page.php");
          break;
      } else {
          // go to default case
      }

    default:
      include("404.php");
    break;
}

break when $val == 7 or else don't break and drop into the default.

An even more hackish way IMO would be goto:

switch ($option) {

    case "a":
      if($val == 7) {
          include("page.php");
      } else {
          goto notfound;
      }
    break;

    case "b":
      include("somefile.php");
    break;

    default:
    notfound:
     include("404.php");
    break;

}

So in short, no you can't jump to another case without falling into it or goto. I would probably create a function and call it in both places.

Upvotes: 2

Related Questions