Current Location: Home> Latest Articles> Understanding PHP continue Statement with Practical Examples

Understanding PHP continue Statement with Practical Examples

M66 2025-07-09

What is the continue Statement in PHP?

In PHP, the continue statement is used to skip the remaining code in the current loop iteration and move directly to the next iteration. It’s commonly used when you want to ignore certain conditions during looping.

Basic Syntax of continue

<span class="fun">continue;</span>

You can also specify the number of loop levels to skip when using nested loops:

<span class="fun">continue 2; // skips the outer loop</span>

Common Use Cases

The continue statement is typically used in scenarios such as:

  • Skipping specific items in a loop based on a condition
  • Avoiding unnecessary processing when certain criteria are met

Example: Skipping Even Numbers

The following example demonstrates how to skip even numbers using continue:

<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i % 2 == 0) {
        continue;
    }
    echo $i . "\n";
}
?>

Output:

1
3
5
7
9

Example: Conditional Output Handling

This example outputs "Fizz" for multiples of 5 and "Buzz" for multiples of 3, skipping the rest of the logic when those conditions are met:

<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i % 5 == 0) {
        echo " Fizz";
        continue;
    } elseif ($i % 3 == 0) {
        echo " Buzz";
        continue;
    }
    echo $i;
}
?>

Output:

<span class="fun">12 Fizz4 Buzz Fizz78 FizzBuzz</span>

Conclusion

The continue statement is a powerful tool for controlling loop execution in PHP. It helps write cleaner and more efficient code by skipping over logic that doesn’t need to run under certain conditions.