From 2ae08531806cf7b7183f7201db7590a025ae66bf Mon Sep 17 00:00:00 2001 From: Torben Wilson Date: Thu, 27 Jan 2000 21:26:37 +0000 Subject: [PATCH] Added notes about optional args to break and continue. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@18860 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/control-structures.xml | 106 ++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 26 deletions(-) diff --git a/language/control-structures.xml b/language/control-structures.xml index 53ed8d48b9..048d57608c 100644 --- a/language/control-structures.xml +++ b/language/control-structures.xml @@ -533,43 +533,97 @@ foreach($a as $k => $v) { - - <literal>break</literal> + + <literal>break</literal> - - break breaks out of the current looping control-structures. - - - + + break ends execution of the current + if, for, + while, or switch + structure. + + + + break accepts an optional numeric argument + which tells it how many nested enclosing structures are to be + broken out of. + + + + + $i = 0; while ($i < 10) { if ($arr[$i] == "stop") { - break; + break; /* You could also write 'break 1;' here. */ } $i++; } - - - - - <literal>continue</literal> - - - continue is used within looping structures to - skip the rest of the current loop iteration and continue - execution at the beginning of the next iteration. + +/* Using the optional argument. */ +$i = 0; +while ( ++$i ) { + switch ( $i ) { + case 5: + echo "At 5<br>\n"; + break 1; /* Exit only the switch. */ + case 10: + echo "At 10; quitting<br>\n"; + break 2; /* Exit the switch and the while. */ + default: + break; + } +} + + + + + + + <literal>continue</literal> + + + continue is used within looping structures to + skip the rest of the current loop iteration and continue execution + at the beginning of the next iteration. + + + + continue accepts an optional numeric argument + which tells it how many levels of enclosing loops it should skip + to the end of. + + + - while (list($key,$value) = each($arr)) { - if ($key % 2) { // skip even members - continue; - } - do_something_odd ($value); - } - - +while (list($key,$value) = each($arr)) { + if ($key % 2) { // skip even members + continue; + } + do_something_odd ($value); +} + +$i = 0; +while ( $i++ < 5 ) { + echo "Outer<br>\n"; + while ( 1 ) { + echo "  Middle<br>\n"; + while ( 1 ) { + echo "  Inner<br>\n"; + continue 3; + } + echo "This never gets output.<br>\n"; + } + echo "Neither does this.<br>\n"; +} + + + + + <literal>switch</literal>