|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 03:00:01 2025 UTC | 
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 ')'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.