How to Use Continue in PHP: Syntax and Examples
In PHP, the
continue statement skips the current loop iteration and moves to the next one. It works inside loops like for, while, and foreach to control flow by ignoring the rest of the code in the current cycle.Syntax
The continue statement can be used alone or with an optional numeric argument to specify how many nested loops to skip to the next iteration.
continue;- skips the current iteration of the innermost loop.continue n;- skips the current iteration of the nth enclosing loop.
php
<?php // Basic continue usage for ($i = 1; $i <= 5; $i++) { if ($i == 3) { continue; // Skip when $i is 3 } echo $i . " "; } // Using continue with a number (nested loops) for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { if ($j == 2) { continue 2; // Skip to next iteration of outer loop } echo "$i,$j "; } } ?>
Output
1 2 4 5 1,1 3,1 3,3
Example
This example shows how continue skips printing the number 3 in a loop from 1 to 5. It also demonstrates skipping inner loop iterations and jumping to the next outer loop cycle.
php
<?php for ($i = 1; $i <= 5; $i++) { if ($i == 3) { continue; // Skip printing 3 } echo $i . " "; } echo "\n"; for ($i = 1; $i <= 2; $i++) { for ($j = 1; $j <= 3; $j++) { if ($j == 2) { continue 2; // Skip to next $i iteration } echo "$i,$j "; } } ?>
Output
1 2 4 5
1,1 2,1 1,3 2,1 2,3
Common Pitfalls
One common mistake is using continue outside of loops, which causes an error. Another is misunderstanding continue n; in nested loops, where n must be a positive integer indicating how many loops to skip. Using a wrong number can lead to unexpected behavior or errors.
Also, confusing continue with break is common: continue skips to the next iteration, while break exits the loop entirely.
php
<?php // Wrong: continue outside loop // continue; // Fatal error: 'continue' not in the 'loop' or 'switch' context // Wrong: continue with invalid number for ($i = 0; $i < 3; $i++) { continue 0; // Warning: 'continue' with non-positive argument } // Correct usage for ($i = 0; $i < 3; $i++) { if ($i == 1) { continue; // Skip iteration when $i is 1 } echo $i . " "; } ?>
Output
0 2
Quick Reference
Continue Statement Cheat Sheet:
| Usage | Effect |
|---|---|
continue; | Skip current iteration of innermost loop |
continue 2; | Skip current iteration of 2nd enclosing loop |
Used inside for, while, foreach | Controls loop flow by skipping code after continue |
| Cannot be used outside loops | Causes fatal error |
| Usage | Effect |
|---|---|
| continue; | Skip current iteration of innermost loop |
| continue 2; | Skip current iteration of 2nd enclosing loop |
| Used inside for, while, foreach | Controls loop flow by skipping code after continue |
| Cannot be used outside loops | Causes fatal error |
Key Takeaways
Use
continue to skip the rest of the current loop iteration and move to the next one.continue n; skips iterations in nested loops, where n is the number of loops to skip.Never use
continue outside of loops; it will cause a fatal error.Remember that
continue skips to the next iteration, while break exits the loop entirely.Use
continue to make loops cleaner by avoiding deeply nested if-else blocks.