Control Structures


continue

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

Syntax Description
for( ; ; )
{
  if(expr)
  {
    stmt1;
    continue;
  }
  stmt2;
}
executes stmt1 and go to the beginning of for loop if expr is TRUE
<?php
  for($i = 1; ; $i++)  // infinite loop
  {
    if($i % 5)
      continue;        // go to the beginning of for loop
    echo "$i\r\n";     // statement is executed if expression is FALSE
    sleep(1);
  }
?>
[result]  
5
10
15
... (repetition)
  • Option of continue
    continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.
<?php
  $j = 0;
  for($i = 0; ; $i++)                             // infinite loop(level 1)
  {
    sleep(1);
    if($i)
      echo "This is for statement \$i = $i\r\n";  // repeated by continue 2
    while(1)                                      // infinite loop(level 2)
    {
      $j++;
      if(($j % 5) == 0)
        continue 2;                               // go to the beginning of for loop 
      echo "$j, ";
      sleep(1);
    }
  }
?>
[result]  
1, 2, 3, 4, This is for statement $i = 1
6, 7, 8, 9, This is for statement $i = 2
11, 12, 13, 14, This is for statement $i = 3
... (repetition)