PHP Continue
Palavras-chave:
Publicado em: 05/08/2025PHP `continue` Statement: Skipping Iterations in Loops
The `continue` statement in PHP is a powerful control structure that allows you to skip the rest of the current iteration of a loop (such as `for`, `while`, `do-while`, and `foreach`) and proceed to the next iteration. This article provides a comprehensive overview of how to use the `continue` statement effectively, including its syntax, use cases, and alternative approaches.
Fundamental Concepts / Prerequisites
Before diving into the specifics of the `continue` statement, it's essential to have a solid understanding of the following:
- Basic PHP syntax and control structures (e.g., `if`, `else`).
- Looping constructs in PHP (`for`, `while`, `do-while`, `foreach`).
- Basic knowledge of CodeIgniter framework (to understand context better). While `continue` is native PHP, understanding how it is used within CI loops is useful.
Core Implementation/Solution
The `continue` statement allows you to skip the remaining code within the current iteration of a loop. The loop will then immediately proceed to the next iteration, if one exists. When used with a `switch` statement, `continue` behaves like `break`. The `continue` statement optionally accepts a numeric argument which tells it how many nested enclosing looping structures to skip to the end of. The default value is 1, skipping to the end of the current loop.
<?php
// Example using a for loop
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
continue; // Skip even numbers
}
echo "Odd number: " . $i . "<br>";
}
// Example inside a CodeIgniter controller
class MyController extends CI_Controller {
public function index() {
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach ($data as $item) {
if ($item % 2 == 0) {
continue; // Skip even numbers
}
echo "Odd number: " . $item . "<br>";
}
}
}
?>
Code Explanation
Let's break down the example code:
First Example: `for` loop. The `for` loop iterates from 1 to 10. Inside the loop, an `if` statement checks if the current number (`$i`) is even using the modulo operator (`%`). If `$i` is even (i.e., `$i % 2 == 0`), the `continue` statement is executed. This immediately skips the rest of the code within the current iteration and proceeds to the next value of `$i`. As a result, only odd numbers are printed to the screen.
Second Example: `foreach` loop inside a CodeIgniter controller. This demonstrates the same concept but within the context of a CodeIgniter controller. A simple array `$data` is iterated through using a `foreach` loop. The logic regarding skipping even numbers using `continue` is identical to the first example. The output mirrors that of the first example, showcasing use within CodeIgniter.
Complexity Analysis
The use of the `continue` statement itself doesn't significantly impact the complexity of the code. The complexity is primarily determined by the loop structure it's used within.
Time Complexity: The time complexity of both examples is O(n), where n is the number of iterations in the loop (10 in the first example). The `continue` statement simply skips certain iterations but doesn't fundamentally alter the number of iterations the loop performs. Inside CodeIgniter, the complexity is still O(n), where 'n' is the number of elements in the `$data` array.
Space Complexity: The space complexity is O(1) (constant) as it doesn't require any additional memory that scales with the input size. The array within the CodeIgniter example remains constant throughout the `foreach` execution.
Alternative Approaches
Instead of using `continue`, you could achieve the same result by inverting the `if` condition. For example, instead of:
if ($i % 2 == 0) {
continue;
}
echo "Odd number: " . $i . "<br>";
You could write:
if ($i % 2 != 0) {
echo "Odd number: " . $i . "<br>";
}
The trade-off is that the logic might become less readable in certain scenarios, especially when dealing with complex conditions. Using `continue` can sometimes improve the clarity of the code by explicitly stating that certain cases should be skipped.
Conclusion
The `continue` statement in PHP provides a way to skip the current iteration of a loop, allowing you to control the flow of execution based on certain conditions. It can be a useful tool for streamlining your code and making it more readable, especially when dealing with complex loop structures. When working within a framework like CodeIgniter, the usage of `continue` remains the same as in native PHP loops, contributing to maintainability and consistency.