note 43232 added to control-structures.break
| From: | php-general at lists dot php dot net | Date: | Mon, 14 Jun 2004 22:21:56 +0000 |
| Subject: | note 43232 added to control-structures.break | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-71069@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/control-structures.break.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43232
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43232&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43232&report=yes
Search -- http://master.php.net/manage/user-notes.php