start learning
Image 1

Continue statement

In PHP, the continue statement is a branching statement used to skip the current iteration of a loop and proceed to the next iteration. It is primarily used within loops to control the flow of your program by allowing you to skip specific iterations based on a condition.

Let's define the continue statement and provide examples with clarifications:


continue in a for loop


for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        continue; // Skip iteration when $i is equal to 3
    }
    echo $i . ' ';
}
 

Clarification: In this example, we have a for loop that iterates from $i = 1 to $i = 5. Inside the loop, there is an if condition that checks if $i is equal to 3. When $i equals 3, the continue statement is executed, skipping the rest of the code in that iteration. As a result, 3 is omitted from the output, and the loop continues with 4 and 5.


continue in a while loop


$num = 1;
while ($num <= 5) {
    if ($num == 4) {
        $num++; // Increment $num before continuing to skip it
        continue; // Skip iteration when $num is equal to 4
    }
    echo $num . ' ';
    $num++;
}
 

Clarification: This example demonstrates the use of continue in a while loop. The loop continues to execute as long as $num is less than or equal to 5. When $num equals 4, the continue statement is executed, but we increment $num first to avoid an infinite loop. This skips the output of 4 and proceeds with 5.


continue in a foreach loop with an array


$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
    if ($number % 2 == 0) {
        continue; // Skip even numbers
    }
    echo $number . ' ';
}
 

Clarification: In this example, we use a foreach loop to iterate through an array of numbers. Inside the loop, there is an if condition that checks if the current number is even (i.e., divisible by 2). When an even number is encountered, the continue statement is executed, skipping the even numbers and printing only the odd numbers.


continue in a do-while loop


$num = 1;
do {
    if ($num % 2 == 0) {
        $num++; // Increment $num before continuing to skip it
        continue; // Skip even numbers
    }
    echo $num . ' ';
    $num++;
} while ($num <= 5);
 

In this example, we use a do-while loop to iterate through numbers from 1 to 5. Inside the loop, we check if the current number is even (divisible by 2). If it's even, we increment $num and use continue to skip even numbers. As a result, only odd numbers are printed.


continue in a nested loop


for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($i == $j) {
            continue 2; // Skip the entire outer loop and inner loop when $i equals $j
        }
        echo "($i, $j) ";
    }
}
 

Clarification: This example features nested loops, and when $i is equal to $j, the continue 2; statement is executed, which skips not only the current iteration of the inner loop but also the entire outer loop. As a result, combinations like (1, 1), (2, 2), and (3, 3) are skipped, and only the remaining combinations are printed.


continue to filter elements in an array


$fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
$filteredFruits = [];

foreach ($fruits as $fruit) {
    if (strlen($fruit) > 5) {
        continue; // Skip fruits with more than 5 characters
    }
    $filteredFruits[] = $fruit;
}
print_r($filteredFruits);
 

In this example, we iterate through an array of fruits using a foreach loop. Inside the loop, we check the length of each fruit's name. If the length is greater than 5 characters, the continue statement is used to skip that fruit. The result is that only fruits with names of 5 characters or less are added to the $filteredFruits array and printed using print_r.


These additional examples illustrate how the continue statement can be applied in various situations, such as filtering elements in an array, handling nested loops, and skipping specific iterations based on conditions.