note 125991 added to control-structures.continue
| From: | claurjimenez at main dot php dot net | Date: | Wed, 07 Apr 2021 15:13:51 +0000 |
| Subject: | note 125991 added to control-structures.continue | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216261@lists.php.net to get a copy of this message | ||
I've been playing around to see what it does in practice. This is what helped me understand
what it does and its difference with using break.
<?php
$i = 0;
while ($i++ < 5) {
while ($i % 2 === 0) {
echo "$i is even. \n";
####
}
echo "$i is odd. \n";
}
?>
Where ####:
- break: outputs both '$i is even' and '$i is odd' for even numbers.
- continue: infinite loop as soon as it evaluates true for the first even number.
- break 2: as soon as it runs, it exits from both loops.
- continue 2: outputs numbers correctly.
What I understand from this is that break will exit current looping structure and will keep running
outer loop code. Continue will make loop get back to evaluation, and will iterate over itself until
it evaluates to false. Break 2 will exit 2 levels, which in this case will stop the iteration
altogether. Continue 2 will evaluate not the current loop (level 1 so to speak), but the outer loop
in this case.
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 149.86.76.35)
----
Manual Page -- http://php.net/manual/en/control-structures.continue.php
Edit -- https://main.php.net/note/edit/125991
Del: integrated -- https://main.php.net/note/delete/125991/integrated
Del: useless -- https://main.php.net/note/delete/125991/useless
Del: bad code -- https://main.php.net/note/delete/125991/bad+code
Del: spam -- https://main.php.net/note/delete/125991/spam
Del: non-english -- https://main.php.net/note/delete/125991/non-english
Del: in docs -- https://main.php.net/note/delete/125991/in+docs
Del: other reasons-- https://main.php.net/note/delete/125991
Reject -- https://main.php.net/note/reject/125991
Search -- https://main.php.net/manage/user-notes.php