Skip to content
Justice Tech School

break, continue and Alternative Syntax

break exits a loop and continue skips a round. break 2 leaves two nested loops. Alternative syntax with colons and endif is handy in templates.

Example
for ($i = 1; $i <= 5; $i++) {
  if ($i == 2) continue;
  if ($i == 4) break;
  echo $i, " ";
}
echo "\n";
foreach ([1, 2] as $a) foreach ([1, 2] as $b) { if ($b == 2) continue 2; echo "$a$b "; }
echo "\n";
if (true): echo "alt syntax\n"; endif;

Result

1 3 
11 21 
alt syntax

Exercise

Skip to the next round.

if ($x) ;

Quiz Test

What does break 2 do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.