note 43232 deleted from control-structures.break by betz
| From: | betz@php.net | Date: | Tue, 15 Jun 2004 06:26:16 +0000 |
| Subject: | note 43232 deleted from control-structures.break by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71076@lists.php.net to get a copy of this message | ||
Note Submitter:
----
In response to abodeman... PHP needs to have a goto, or at least a labelled loop. This is a far
nicer way to exit a few nested loops than any other method. As you've illustrated, 'break
x' is nasty because you have to change it if you change the level of nesting for the break
statement. Exceptions work with functions, not loops. The only other way is to have a very ugly
thing like so:
<?php
$done1 = $done2 = $done3 = FALSE;
for ($x=0; $x <= 4 && !$done1; $x++) {
echo 'Outer loop: ' . $x . ' of 4.<br>';
for ($y=0; $y <= 4 && !$done2; $y++) {
echo 'Middle loop: ' . $y . 'of 4.<br>';
for ($z=0; $z <= 4 && !$done3; $z++) {
echo 'Inner loop: ' . $z . 'of 4.<br>';
if (myFunc == 0) {
echo 'Sudden exit!<br>';
$done1 = $done2 = $done3 = TRUE; // Yuk.
}
}
}
}
?>
whereby you have to create a new boolean for each loop you need to exit out of if you need to
suddenly exit.
I don't understand why PHP doesn't at least have labelled loops.