php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44369 PHP Loop Labels
Submitted: 2008-03-08 06:56 UTC Modified: 2008-03-08 18:16 UTC
From: php at xyzzy dot cjb dot net Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.5 OS: Mac OS X 10.5.2
Private report: No CVE-ID: None
 [2008-03-08 06:56 UTC] php at xyzzy dot cjb dot net
Description:
------------
This is an enhancement request to add loop labels to PHP. Currently, to 
break out of nested loops, PHP allows you to specify a number to a break 
or continue statement. It would increase code clarity and 
maintainability to be able to specify a textual label of the loop to 
continue or break out of.

This is similar to Bug #29287 "Request: Line labels and goto" except 
that I'm not requesting goto, which is what that bug was mostly used to 
discuss before being closed. This ticket is specifically for the feature 
of adding loop labels, which was not adequately discussed in #29287.





Reproduce code:
---------------
An example of a nested loop (admittedly not a very useful example), how PHP currently allows you to write it:


for ($i = 0; $i < 5; ++$i)
{
    print "$i: ";
    for ($j = 0; $j < 5; ++$j)
    {
        print "$j ";
        if ($j == 2 && $i == 4)
            continue 2;
    }
    print "\n";
}

How I would like to be able to write it:

ROW: for ($i = 0; $i < 5; ++$i)
{
    print "$i: ";
    COLUMN: for ($j = 0; $j < 5; ++$j)
    {
        print "$j ";
        if ($j == 2 && $i == 4)
            continue ROW;
    }
    print "\n";
}



Expected result:
----------------
Both examples should behave the same, if this feature is implemented.

Actual result:
--------------
Parse error: syntax error, unexpected ':' in /private/tmp/a.php on line 
2









Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-08 11:41 UTC] johannes@php.net
PHP 6 has goto which can be used for that.
 [2008-03-08 18:16 UTC] php at xyzzy dot cjb dot net
How can this code be rewritten using goto? I downloaded a PHP 6 snapshot and tried this:

ROW: for ($i = 0; $i < 5; ++$i)
{
    print "$i: ";
    COLUMN: for ($j = 0; $j < 5; ++$j)
    {
        print "$j ";
        if ($j == 2 && $i == 4)
            goto ROW;
    }
    print "\n";
}

but the entire loop is repeated; it doesn't produce the same output as the first example. 

From http://www.php.net/~derick/meeting-notes.html#adding-goto (first result on Google for "php 6 goto"), I see that there were plans to "reuse the break keyword, and extend it with a static label.", but this does not appear to be implemented the the latest snapshot. Those meeting notes were from 2005 however. Google also found http://php6dev.blogspot.com/ but that page is currently down so I couldn't read it.

How can I use goto to act as "continue 2" or "break 2"? I also tried:

for ($i = 0; $i < 5; ROW: ++$i)
{
    print "$i: ";
    for ($j = 0; $j < 5; ++$j)
    {
        print "$j ";
        if ($j == 2 && $i == 4)
            goto ROW;
    }
    print "\n";
}

and: for ($i = 0; ROW: $i < 5; ++$i)
and: for (ROW: $i = 0; $i < 5; ROW: ++$i)

but these all report:

Parse error: syntax error, unexpected ':', expecting ')'
 [2012-10-26 08:18 UTC] root at bugs dot php dot net
Did you consider this:

for ($i = 0; $i < 5; ++$i)
{
    print "$i: ";

    for ($j = 0; $j < 5; ++$j)
    {
        print "$j ";
        if ($j == 2 && $i == 4)
            goto END;
    }

    print "\n";
    END:
}

If the label needs to proceed a statement, try LABEL: noop();

Another solution could be to get the the nesting level and use:

continue $nest_level - 1;

Unfortunately, I can find no such function so this would need to be done 
manually.
 [2014-09-27 10:15 UTC] cameron dot adam+php at gmail dot com
If this was to be implemented (it should), then perhaps follow Java's lead for the syntax: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.7

Various other languages have different implementation of this, and I'd worry PHP might accidentally end up with something like how Ruby does it (http://www.ruby-doc.org/core-2.1.3/Kernel.html#method-i-catch) which is awful.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 13:01:28 2024 UTC