PHP Continue
The continue statement in PHP is used to skip the remaining code of the current iteration within a loop (for, while, do-while, or foreach) and immediately jump to the beginning of the next iteration.
Continue in For Loops
The continue statement is used within a for loop to skip the remaining code of the current iteration and immediately proceed to the next iteration.
Example
Move to next iteration if $x = 4:
<!DOCTYPE html> <html> <body> <?php for ($x = 0; $x < 10; $x++) { if ($x == 4) { continue; } echo "The number is: $x
"; } ?> </body> </html>
Output
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
• Continue in While Loop
The continue statement is a PHP while loop is used to skip the remaining code within the current iteration of the loop and immediately jump to the beginning of the next iteration.
Example
Move to next iteration if $x = 4:
<!DOCTYPE html> <html> <body> <?php $x = 0; while($x < 10) { $x++; if ($x == 4) { continue; } echo "The number is: $x
"; } ?> </body> </html>
Output
The number is: 1
The number is: 2
The number is: 3
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
• Continue in Do While Loop
The continue statement stops the current iteration in the do...while loop and continue with the next.
Example
Stop, and jump to the next iteration if $i is 3:
<!DOCTYPE html> <html> <body> <?php $i = 0; do { $i++; if ($i == 3) continue; echo $i; } while ($i < 6); ?> </body> </html>
Output
12456
• Continue in For Each Loop
The continue statement stops the current iteration in the foreach loop and continue with the next.
Example
Stop, and jump to the next iteration if $x is "blue":
<!DOCTYPE html> <html> <body> <?php $colors = array("red", "green", "blue", "yellow"); foreach ($colors as $x) { if ($x == "blue") continue; echo "$x
"; } ?> </body> </html>
Output
red
green
yellow