Req #44369 [Com]: PHP Loop Labels

From: Date: Sat, 27 Sep 2014 10:15:32 +0000
Subject: Req #44369 [Com]: PHP Loop Labels
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187732@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=44369&edit=1 ID: 44369 Comment by: cameron dot adam+php at gmail dot com Reported by: php at xyzzy dot cjb dot net Summary: PHP Loop Labels Status: Not a bug Type: Feature/Change Request Package: Feature/Change Request Operating System: Mac OS X 10.5.2 PHP Version: 5.2.5 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2012-10-26 08:18:11] 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. ------------------------------------------------------------------------ [2008-03-08 18:16:11] 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 ')' ------------------------------------------------------------------------ [2008-03-08 11:41:48] johannes@php.net PHP 6 has goto which can be used for that. ------------------------------------------------------------------------ [2008-03-08 06:56:00] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=44369&edit=1

« previous php.bugs (#187732) next »