Menu
Coddy logo textTech

Continue Statement

Part of the Fundamentals section of Coddy's PHP journey — lesson 52 of 71.

While break exits a loop entirely, sometimes you only want to skip the current iteration and move on to the next one. That's exactly what the continue statement does.

When PHP encounters continue, it immediately jumps to the next iteration of the loop, skipping any remaining code in the current iteration.

<?php
for ($i = 1; $i <= 5; $i++) {
    if ($i === 3) {
        continue;
    }
    echo $i . "\n";
}
?>

This outputs:

1
2
4
5

Notice that 3 is missing. When $i equals 3, the continue statement skips the echo and moves directly to the next iteration where $i becomes 4.

A common use case is filtering out unwanted values while processing an array:

<?php
$numbers = [10, -5, 8, -3, 12];

foreach ($numbers as $num) {
    if ($num < 0) {
        continue;
    }
    echo $num . "\n";
}
?>

This prints only the positive numbers: 10, 8, and 12. The negative values are skipped, but the loop keeps running through all elements.

The key difference: break stops the loop completely, while continue only skips to the next iteration.

challenge icon

Challenge

Easy

Read two lines of input:

  1. A comma-separated list of integers (e.g., 3,7,12,5,8,20,15)
  2. A divisor number

Use a foreach loop to iterate through the array. Use continue to skip any numbers that are divisible by the given divisor. Print all other numbers, each on a separate line.

Example 1:

If the inputs are 1,2,3,4,5,6,7,8,9,10 and 3, the output should be:

1
2
4
5
7
8
10

(Numbers 3, 6, and 9 are skipped because they are divisible by 3)

Example 2:

If the inputs are 10,15,20,25,30 and 5, the output should be:

(All numbers are divisible by 5, so nothing is printed)

Example 3:

If the inputs are 7,14,21,10,5 and 7, the output should be:

10
5

Cheat sheet

The continue statement skips the current iteration of a loop and moves to the next one, without exiting the loop entirely.

Basic example with for loop:

<?php
for ($i = 1; $i <= 5; $i++) {
    if ($i === 3) {
        continue;
    }
    echo $i . "\n";
}
?>

This outputs 1, 2, 4, 5 (skipping 3).

Using continue with foreach to filter array values:

<?php
$numbers = [10, -5, 8, -3, 12];

foreach ($numbers as $num) {
    if ($num < 0) {
        continue;
    }
    echo $num . "\n";
}
?>

This prints only positive numbers (10, 8, 12), skipping negative values.

Key difference: break exits the loop completely, while continue only skips to the next iteration.

Try it yourself

<?php
// Read input
$input = trim(fgets(STDIN));
$divisor = intval(trim(fgets(STDIN)));

// Convert comma-separated string to array of integers
$numbers = array_map('intval', explode(',', $input));

// TODO: Write your code below
// Use a foreach loop to iterate through $numbers
// Use continue to skip numbers divisible by $divisor
// Print all other numbers, each on a separate line

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals