Loops · Lesson 6 of 6
continue
Skip the rest of one iteration and move to the next.
Lesson
continue skips the remaining statements in the current loop iteration. It can keep a loop shallow when a value should be ignored, but avoid stacking many continue conditions because the main path becomes difficult to see. Validate or filter a collection before iterating when that produces simpler code.
The example prints only positive values.
Further guidance
Before running a loop, identify the value that changes and the condition that eventually stops it. Check empty collections and one-item collections as well as the normal case. Prefer foreach when iterating a collection, and keep early exits easy to understand.
Practice task: Create a fresh example for continue. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.
Code examples
continue
PHP
foreach ([-2, 3, 0, 5] as $value) {
if ($value <= 0) {
continue;
}
echo $value, ' ';
}
Expected output
3 5
Key points
- Skip the rest of one iteration and move to the next.
- Identify the loop state and a clear termination condition.
- Test empty and one-item inputs as well as the normal case.
Your learning progress
Finish this lesson
Optional practice
PHP playground
This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.
Execution is disabled. No code is sent to a server.