How to write a continue statement in PHP

The continue statement skips over an iteration of a loop when the condition is satisfied. In PHP, the continue statement takes in an optional parameter of the number of levels in the enclosing loops that need to be skipped. The default value resumes the execution from the beginning of the next iteration of the current loop. The continue statement moves to the next iteration of the loop with any updated values of variables inside the loop block.

The following illustration shows the control flow of the continue​ statement with the default parameter:

svg viewer

Code

Let’s see how the continue statement is used in the actual code.

1. Using continue without an argument

The loop skips the iteration when num % 2 is not zero (i.e.​, the number is odd):

<?php
for ($num = 0; $num <= 10; $num++) {
if ($num % 2)
continue;
echo "$num\n";
}
?>

2. Using continue with an argument

continue 2 means that two levels of the enclosing loops need to be skipped over. “Hello from the outer loop” is never printed because whenever j equals two, the execution is skipped over to line 3, ignoring the code below.

<?php
$i = 0;
while ( $i < 3) {
$i++;
$j = 0;
while ( $j <= 3) {
$j++;
if ($j == 2) {
continue 2;
}
echo "Hello from the inner loop!\n";
}
echo "Hello from the outer loop!\n";
}
?>

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved