在条件内打破案例(在交换机中)

时间:2016-10-17 14:04:57

标签: switch-statement

在我提出这个问题之前,我尝试使用PHP并且它有效,但我想知道它是否被认为是标准的。

所以在这种情况下,我有一个巨大的switch / case语句,每个case基于请求头处理特定的HTTP请求。在一种情况下,如果变量为true,我想更新mysql表中的行,如果它是假的则删除行,所以我想知道我是否可以这样做:

<?php
switch ($request) {
    case 'stuff':
        stuff();
        break;
    case 'thatThing':
        if (!$theVar) {
            deleteTheRows();
            // Break out of the switch statement so that the rest of this case is ignored
            break;
        }
        updateTheRows();
        break;
    default:
        otherStuff();
        break;
}
?>

我的想法是让break;return那样行动,让我在处理正常情况的代码之前处理所有异常情况,而无需将其嵌套在另外压痕深度

1 个答案:

答案 0 :(得分:0)

In one case I want to update rows in a mysql table if a variable is true
and delete rows if it's false

其他原因同样有效......

<?php
switch ($request) {
    case 'stuff':
        stuff();
        break;
    case 'thatThing':
        if (!$theVar) {
            deleteTheRows();
        } else {
            updateTheRows();
        }
        break;
    default:
        otherStuff();
        break;
}
?>