rfc:continue_on_switch_deprecation

This is an old revision of the document!


PHP RFC: Deprecate and remove continue targeting switch

Introduction

In PHP, if continue is applied to a switch statement, it behaves the same as break. In other languages, it would continue the surrounding loop instead. To avoid confusion, this RFC proposes to deprecate and remove continues acting on switch.

The following example demonstrates the current behavior:

while ($foo) {
    switch ($bar) {
        case "baz":
            continue; // In PHP: Behaves like "break;"
                      // In C: Behaves like "continue 2;"
    }
}

While continue and break are synonymous in this context, break is used almost exclusively, by convention and due to familiarity from other languages. Attempts to use continue are almost certainly attempts to achieve the behavior of continue 2 by programmers who are not familiar with this PHP-ism.

Proposal

This RFC proposes to deprecate in PHP 7.3 and remove in PHP 8 the ability to target a switch statement through continue. This is no loss in functionality (it is always possible to replace the continue with a break), but avoids a gotcha for programmers coming from other languages.

The following illustrates cases which are deprecated and by what they can be replaced at various levels of loop nesting:

switch ($foo) {
    case "bar":
        continue; // Deprecated
        break;    // Use this for equivalent behavior
}
 
while ($foo) {
    switch ($bar) {
        case "baz":
            continue;   // Deprecated
            break;      // Use this for equivalent behavior
            continue 2; // Or this for C-like behavior
    }
}
 
while ($foo) {
    switch ($bar) {
        case "baz":
            while ($xyz) {
                continue 2; // Deprecated
                break 2;    // Use this for equivalent behavior
                continue 3; // Or this for C-like behavior
            }
    }
}

Backward Incompatible Changes

PHP 7.3 throws additional deprecation warnings, PHP 8 generates a compile error. Fixing it is always trivial by replacing continue with break.

Unaffected PHP Functionality

Continue can still be used inside switch statements, as long as it does not target the switch.

Vote

As this is a language change, a 2/3 majority is required.

rfc/continue_on_switch_deprecation.1529854416.txt.gz · Last modified: 2018/06/24 15:33 by nikic