PHP Continue Statement

Welcome to The Coding College! In this tutorial, we’ll explore the PHP continue statement, a useful tool for skipping the rest of the current iteration in a loop and moving to the next one. With continue, you can streamline your loop logic by bypassing specific iterations based on conditions.

What Is the PHP Continue Statement?

The continue statement is used in loops to skip the remaining code for the current iteration and immediately move to the next iteration. It works with all loop types in PHP (for, while, do...while, and foreach).

Syntax

continue;

How Does continue Work?

When the continue statement is encountered, PHP stops executing the rest of the code within the current iteration of the loop and jumps to the next iteration.

Example: Using continue in a for Loop

<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i % 2 == 0) {
        continue; // Skip even numbers
    }
    echo "Number: $i<br>";
}
?>

Output:

Number: 1  
Number: 3  
Number: 5  
Number: 7  
Number: 9  

Explanation:

  • The loop runs from 1 to 10.
  • When $i % 2 == 0 (even numbers), the continue statement skips to the next iteration, ignoring the echo statement.

Example: Using continue in a while Loop

<?php
$count = 0;

while ($count < 10) {
    $count++;
    if ($count == 5) {
        continue; // Skip when $count equals 5
    }
    echo "Count: $count<br>";
}
?>

Output:

Count: 1  
Count: 2  
Count: 3  
Count: 4  
Count: 6  
Count: 7  
Count: 8  
Count: 9  
Count: 10  

Example: Using continue in a foreach Loop

<?php
$numbers = [1, 2, 3, 4, 5, 6];

foreach ($numbers as $number) {
    if ($number % 2 == 0) {
        continue; // Skip even numbers
    }
    echo "Odd Number: $number<br>";
}
?>

Output:

Odd Number: 1  
Odd Number: 3  
Odd Number: 5 

Using continue with Labels for Nested Loops

In PHP, you can use continue with a numeric argument or labels to specify which loop to skip when working with nested loops.

Example: Skipping the Inner Loop

<?php
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($j == 2) {
            continue; // Skip $j = 2
        }
        echo "Outer: $i, Inner: $j<br>";
    }
}
?>

Output:

Outer: 1, Inner: 1  
Outer: 1, Inner: 3  
Outer: 2, Inner: 1  
Outer: 2, Inner: 3  
Outer: 3, Inner: 1  
Outer: 3, Inner: 3  

Example: Skipping Outer Loops with Numeric Argument

<?php
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($j == 2) {
            continue 2; // Skip the rest of the current outer and inner loops
        }
        echo "Outer: $i, Inner: $j<br>";
    }
}
?>

Output:

Outer: 1, Inner: 1  
Outer: 2, Inner: 1  
Outer: 3, Inner: 1  

Real-World Example: Skipping Invalid Data

You can use continue in practical situations, such as skipping invalid inputs during data processing.

<?php
$emails = ["[email protected]", "", "[email protected]", "invalid", "[email protected]"];

foreach ($emails as $email) {
    if (empty($email) || !str_contains($email, "@")) {
        continue; // Skip empty or invalid emails
    }
    echo "Valid Email: $email<br>";
}
?>

Output:

Valid Email: [email protected]  
Valid Email: [email protected]  
Valid Email: [email protected]  

Differences Between break and continue

Featurebreakcontinue
PurposeExits the entire loopSkips the current iteration
BehaviorEnds execution of the loop completelyMoves to the next iteration
Use CaseStop processing under certain conditionsSkip over specific iterations

Best Practices

  1. Use Conditions Wisely: Ensure continue is used with meaningful conditions to avoid unnecessary skipping of iterations.
  2. Avoid Overusing continue: Overusing it can make the code harder to read and maintain.
  3. Label Nested Loops Properly: When working with nested loops, clearly label the loops if you use continue with a numeric argument.

Common Mistakes

  1. Skipping Important Logic: Be cautious that skipping iterations doesn’t lead to missed data processing.
  2. Infinite Loops: Ensure loop conditions are updated correctly, especially in while loops, to avoid infinite loops caused by skipped iterations.

Conclusion

The continue statement is a powerful tool for controlling loop behavior in PHP. It allows you to skip specific iterations and focus on the logic that matters. By using continue effectively, you can write cleaner and more efficient code.

For more PHP tutorials and programming tips, visit The Coding College. Let’s make coding easier together!

Leave a Comment