[PHP-BUG] Req #75036 [NEW]: Named Continue/Break

[email protected] ("php at haravikk dot me") Fri, 4 Aug 2017 14:27:17 GMT
Newsgroups php.standards
Message-ID <[email protected]>
From:             php at haravikk dot me
Operating system: 
PHP version:      Next Major Version
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:Named Continue/Break

Description:
------------
So I have (somehow) managed for years to develop in PHP without ever
realising that the continue keyword oddly considers a switch statement
to be a looping construct. This is a very strange behaviour, and
completely functionally useless, but I fear the ship has long sailed on
removing it at this point.

To work with switch statements in loops thus requires a numbered
continue/break to ensure it applies to the correct construct, however
this is a bit unwieldy and has the potential for errors if the depth of
any statement changes, and for the most part it's not a very intuitive
way to do it when nested structures are usually fairly simple.

What I'd like to propose is that the continue and break keywords gain
the ability to optionally specify a construct name. For example:

<?php
    for ($i = 0; $i < 10; $i += 1) {
        switch ($i) {
            case 5:
                continue for;
            break;
        }
    }
?>

Here, PHP would encounter the presence of the "for" keyword, and use it
to search for an enclosing for loop, and automatically set the continue
correctly (equivalent to setting "continue 2" manually).

A number can still be specified in this form, but applies only to the
type of construct named. For example:

<?php
    for ($i = 0; $i < 10; $i += 1) {
        for ($j = 0; $j < 10; $j += 1) {
            switch ($j) {
                case 5:
                    continue for 2;
                break;
            }
        }
    }
?>

Here the continue now applies to the outer for loop, rather than the
inner for loop. If I were to set this to "continue for 3", an error
would occur (no more for loops).

This is hopefully a bit easier to use, as it means that PHP does the
actual calculation to decide what value is required to continue the
correct loop type. This could be even easier if we could also leverage
labels, like so:

<?php
    outer: for ($i = 0; $i < 10; $i += 1) {
        inner: for ($j = 0; $j < 10; $j += 1) {
            switch ($j) {
                case 5:
                    continue outer;
                break;
            }
        }
    }
?>

Here both for loops are labelled, inner and outer as appropriate, and we
use the label name on the continue in order to reference the correct
one.

The main problem is that this is a bit more complex syntactically as the
label MUST immediately precede an enclosing loop, or else PHP would not
know what to do. However, this is by far the simplest/most flexible
option, as it requires no counting at all by the developer, as PHP can
identify which loop was meant, and continue/break it correctly.


-- 
Edit bug report at https://bugs.php.net/bug.php?id=75036&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=75036&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=75036&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=75036&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=75036&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=75036&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=75036&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=75036&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=75036&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=75036&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=75036&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=75036&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=75036&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=75036&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75036&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=75036&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=75036&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=75036&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75036&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=75036&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=75036&r=mysqlcfg