continue
As a PHP developer, you may have used loops to iterate through arrays or perform other tasks. The "continue" keyword is a control structure in PHP that allows
The PHP "continue" Keyword: A Comprehensive Guide
As a PHP developer, you may have used loops to iterate through arrays or perform other tasks. The continue keyword is a control structure in PHP that immediately jumps to the next iteration when a specified condition is met. In this article, we will explore the syntax and usage of the continue keyword in depth, and provide plenty of examples to help you master this important PHP feature.
Syntax
The continue keyword is used to skip the current iteration and proceed to the next one in PHP. Here is the basic syntax for using the continue keyword in PHP:
The syntax of PHP continue
continue;In this example, the continue keyword skips the current iteration and moves on to the next one.
Examples
Let's look at some practical examples of how the "continue" keyword can be used:
Examples of PHP continue
<?php
// Example 1
for ($i = 0; $i < 10; $i++) {
if ($i == 5) {
continue;
}
echo $i . PHP_EOL;
}
// Output: 0 1 2 3 4 6 7 8 9
// Example 2
$myArray = ["apple", "banana", "cherry", "date"];
foreach ($myArray as $value) {
if ($value == "cherry") {
continue;
}
echo $value . PHP_EOL;
}
// Output: apple banana dateIn these examples, the continue keyword bypasses specific iterations when a condition is met.
Advanced Usage and Comparison
Skipping Nested Loops
By default, continue only affects the innermost loop. To skip iterations of an outer loop, pass an optional numeric argument that tells PHP how many enclosing loop levels to apply the continue to:
<?php
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
if ($j == 2) {
continue 2; // skip to the next iteration of the OUTER loop
}
echo "i=$i, j=$j" . PHP_EOL;
}
}
// Output:
// i=1, j=1
// i=2, j=1
// i=3, j=1Because continue 2 jumps to the next iteration of the outer loop, the inner loop never reaches $j == 3. A plain continue (or continue 1) would only skip the current inner iteration and the inner loop would still print j=3.
continue vs break
Beginners often confuse these two control structures. While continue skips the current iteration and proceeds to the next one, break immediately terminates the entire loop. Use continue to filter items within a loop, and break to exit early once you have what you need.
<?php
foreach ([1, 2, 3, 4, 5] as $n) {
if ($n == 3) {
continue; // skip 3, keep looping
}
echo $n . PHP_EOL;
}
// Output: 1 2 4 5
foreach ([1, 2, 3, 4, 5] as $n) {
if ($n == 3) {
break; // stop the loop at 3
}
echo $n . PHP_EOL;
}
// Output: 1 2A common gotcha: continue inside switch
Inside a switch statement, continue behaves like break and only exits the switch. PHP even emits a warning. When you are inside a loop and a switch and want to skip the loop iteration, use continue 2:
<?php
foreach (["save", "skip", "delete"] as $action) {
switch ($action) {
case "skip":
continue 2; // skip this loop iteration, not just the switch
default:
echo "Handling: $action" . PHP_EOL;
}
}
// Output:
// Handling: save
// Handling: deleteBenefits
Using the continue keyword has several benefits, including:
- Improved code efficiency: It helps you bypass unnecessary iterations, making your code run more efficiently.
- Simplified logic: It allows you to handle conditional filtering directly within the loop, reducing the need for complex nested
if-elseblocks.
Conclusion
In conclusion, the continue keyword is a powerful tool for PHP developers, allowing them to bypass specific loop iterations and improve the efficiency and readability of their code. Remember that it works in every loop type — for, foreach, while, and do...while — and that the optional level argument (continue 2) is what lets you reach across nested loops.
Related Topics
- PHP Loops overview — how all of PHP's looping constructs fit together.
- PHP
break— the counterpart that exits a loop entirely. breakandcontinuetogether — controlling loop flow side by side.