How to use 'PHP for continue' with Examples
* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.
When writing loops in PHP, there are situations where you might want to skip the current iteration and continue with the next one. The continue
statement is a powerful tool that allows you to do just that. In this blog post, we will dive into the usage of the continue
statement in PHP, understand its benefits, and see practical examples that demonstrate its use.
What is the continue
Statement?
The continue
statement is used within looping structures such as for
, foreach
, while
, and do-while
loops. When continue
is encountered, it immediately stops the execution of the current iteration and proceeds to the next iteration of the loop.
Here’s the basic syntax of continue
:
continue;
Using continue
in Different Loops
Example 1: Using continue
in a for
Loop
Let’s start with a simple example using a for
loop. Suppose we want to print numbers from 1 to 10 but skip the number 5.
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
continue; // Skip the number 5
}
echo $i . " ";
}
Output:
1 2 3 4 6 7 8 9 10
In this example, when $i
equals 5, the continue
statement skips the rest of the loop body and proceeds to the next iteration, effectively omitting the number 5 from the output.
Example 2: Using continue
in a while
Loop
Now, let’s use continue
in a while
loop. Suppose we have an array of numbers and we want to print all numbers except those that are negative.
$numbers = [1, -2, 3, -4, 5, -6];
$i = 0;
while ($i < count($numbers)) {
if ($numbers[$i] < 0) {
$i++;
continue; // Skip negative numbers
}
echo $numbers[$i] . " ";
$i++;
}
Output:
1 3 5
Here, continue
skips the negative numbers in the array and proceeds with the next iteration of the loop.
Example 3: Using continue
in a foreach
Loop
The foreach
loop is commonly used for iterating over arrays. Let’s see how continue
can be used to skip specific values.
$fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
foreach ($fruits as $fruit) {
if ($fruit == 'cherry') {
continue; // Skip 'cherry'
}
echo $fruit . " ";
}
Output:
apple banana date elderberry
In this example, when the loop encounters ‘cherry’, it skips it and continues with the next fruit in the array.
Nested Loops and continue
The continue
statement can also be used in nested loops. By default, continue
affects the innermost loop it is called in. However, you can specify the level of the loop to continue by providing an optional numeric argument.
Example 4: Using continue
in Nested Loops
Let’s consider a scenario where we have nested loops and we want to skip an iteration of the outer loop based on a condition in the inner loop.
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
if ($j == 2) {
continue 2; // Skip the current iteration of the outer loop
}
echo "i = $i, j = $j" . PHP_EOL;
}
}
Output:
i = 1, j = 1
i = 2, j = 1
i = 3, j = 1
In this example, continue 2
skips the current iteration of the outer loop when $j
equals 2.
Conclusion
The continue
statement in PHP is a valuable control structure for managing loop iterations efficiently. By skipping specific iterations, you can streamline your code and avoid unnecessary operations. Whether you are working with for
, while
, foreach
, or nested loops, understanding how to use continue
effectively will make your code more readable and efficient.
Experiment with continue
in your projects to see how it can simplify your loop logic and enhance your coding skills.