Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jump to another case in PHP switch statement

Say I have something like this:

switch ($_GET['func']) {
    case 'foo':
        dobar();
        break;
    case 'orange':
        if ($_GET['aubergine'] == 'catdog') {
            // **DO DEFAULT OPTION**
        } else {
            dosomethingElse();
        }
        break;
    default:
        doDefault();
}

How can I jump to the default case from the marked spot in case 'orange'?

like image 847
Fela Maslen Avatar asked May 02 '11 15:05

Fela Maslen


2 Answers

Try:

switch($_GET['func']) {
  case 'foo':
    dobar();
    break;

  case 'orange':
    if($_GET['aubergine'] != 'catdog') {
        dosomethingElse();
        break;
    }

  default:
    doDefault();
}
like image 125
Yoshi Avatar answered Sep 28 '22 17:09

Yoshi


Yo dawg, you can use a cool new feature called the goto.

<?php
$func = "orange";
function doDefault() { echo "yeehaw!"; return; }
function dobar() { return; }
function dosomethingElse() { return; }

switch ($func) {
    case 'foo':
        dobar();
        break;
    case 'orange':
        if (true) {
            goto defaultlabel;
        } else {
            dosomethingElse();
        }
        break;
    default:
    defaultlabel:
        doDefault();
}
like image 44
2 revs, 2 users 74% Avatar answered Sep 28 '22 16:09

2 revs, 2 users 74%