rfc:continue_on_switch_deprecation

PHP RFC: Deprecate and remove continue targeting switch

Based on the RFC discussion, we decided to instead implement this as a simple warning, which has happened in https://github.com/php/php-src/pull/3364.

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 code 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. The meaning of continue and break inside switch never changes, some cases are just forbidden.

To further clarify which uses of continue are affected, please consider the following example:

while ($foo) {
    switch ($bar) {
        case "baz":
            while ($xyz) {
                continue;   // Targeting the inner while loop: Allowed
                continue 2; // Targeting the switch: Deprecated
                continue 3; // Targeting the outer while loop: Allowed
            }
    }
}

Vote

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

rfc/continue_on_switch_deprecation.txt · Last modified: 2018/07/07 09:18 by nikic