The PHP Continue Statement
The PHP continue statement is used to skip the current iteration of a loop, and continue with the next iteration.
The continue statement is used in:
forloopswhileloopsdo...whileloopsforeachloops
PHP continue in For Loops
The continue statement skips the current iteration in the for loop and continue with the next.
Example
Skip, and move to next iteration if $x = 4:
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
PHP continue in While Loop
The continue statement skips the current iteration in the while loop and continue with the next.
Example
Skip, and move to next iteration if $x = 4:
$x = 0;
while($x < 10) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
$x++;
}
PHP continue in Do While Loop
The continue statement skips the current iteration in the do...while loop and continue with the next.
Example
Skip, and move to the next iteration if $i is 3:
$i = 0;
do {
$i++;
if ($i == 3) continue;
echo $i;
} while ($i < 6);
PHP continue in Foreach Loop
The continue statement skips the current iteration in the foreach loop and continue with the next.
Example
Skip, and move to the next iteration if $value is "blue":
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
if ($value == "blue") continue;
echo "$value<br>";
}