php
  1. php-continue

PHP Continue Statement

In PHP programming, the continue statement is used to skip the current iteration of a loop and move on to the next iteration. It can be used within for, foreach, and while loops.

Syntax

continue;

Example

for($i = 1; $i <= 10; $i++) {
   if($i == 5) {
      continue;
   }
   echo $i . " ";
}

Output

1 2 3 4 6 7 8 9 10 

Explanation

In the example above, we create a for loop that starts at 1 and ends at 10. Inside the loop, we check if the current value of $i is equal to 5. If it is, we execute the continue statement which skips the current iteration and moves on to the next iteration. If the current value of $i is not equal to 5, we print the value of $i to the screen.

As a result, the output skips the number 5 and prints all other values from 1 to 10.

Use

The continue statement can be used in situations where you want to skip a specific iteration of a loop and move on to the next iteration. This can be useful when you're working with arrays or when you need to perform a specific action only for a subset of loop iterations.

Important Points

  • The continue statement can only be used within loops.
  • The continue statement skips the current iteration and moves on to the next iteration.
  • The continue statement can be useful when you need to skip specific loop iterations.

Summary

In PHP programming, the continue statement is used to skip the current iteration of a loop and move on to the next iteration. It is useful when you need to skip specific loop iterations and can be used within for, foreach, and while loops. Remember that the continue statement can only be used within loops.

Published on: