15 of 410 menu

The Continue Construct

The continue construct allows you to skip the remaining part of the current loop iteration and immediately move to the next iteration. It can be used in all types of loops: for, while, do-while, and foreach.

After the continue command, you can specify a number that indicates how many nested loops should be skipped (default is 1).

Syntax

continue;
continue $level;

Example

Skipping even numbers in a loop:

<?php for ($i = 0; $i < 5; $i++) { if ($i % 2 == 0) { continue; } echo $i; } ?>

Code execution result:

13

Example

Using continue in a foreach loop:

<?php $arr = [1, 2, 3, 4, 5]; foreach ($arr as $value) { if ($value == 3) { continue; } echo $value; } ?>

Code execution result:

1245

Example

Using continue with a parameter to skip multiple nesting levels:

<?php for ($i = 0; $i < 3; $i++) { echo "i: $i\n"; for ($j = 0; $j < 3; $j++) { if ($j == 1) { continue 2; } echo "j: $j\n"; } } ?>

Code execution result:

i: 0 j: 0 i: 1 j: 0 i: 2 j: 0

See Also

  • the break construct,
    which completely terminates the loop execution
  • the return construct,
    which terminates function execution
byenru