开发者

Jump to another case in PHP switch statement

开发者 https://www.devze.com 2023-03-02 20:05 出处:网络
Say I have something like this: switch ($_GET[\'func\']) { case \'foo\': dobar(); 开发者_如何转开发break;

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'?


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();
}


Try:

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

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

  default:
    doDefault();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消