Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to write a continue statement in PHP

Educative Answers Team

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


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):

for ($num = 0; $num <= 10; $num++) {
    if ($num % 2)
    echo "$num\n";
Printing even numbers from 0 to 10.

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.

$i = 0;
while ( $i < 3) {
  $j = 0;
  while ( $j <= 3) {
    if ($j == 2) {
      continue 2;
    echo "Hello from the inner loop!\n";
  echo "Hello from the outer loop!\n";
Printing messages inside the loops.


Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring