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.