Home >Backend Development >PHP Tutorial >How to use continue in php

How to use continue in php

下次还敢
下次还敢Original
2024-04-29 10:36:13639browse

continue is used to skip the remaining statements in the current loop and continue executing the next cycle. It is often used to detect conditions to skip portions of a loop or to perform different actions based on conditions.

How to use continue in php

Usage of continue in PHP

What is continue?

continue keyword is used to skip the remaining statements in the current loop and continue executing the next cycle.

Syntax:

<code class="php">continue;</code>

When to use continue?

continue is typically used in the following situations:

  • When a specific condition is detected within a loop and the remainder of the loop needs to be skipped.
  • When you need to perform different operations based on certain conditions in a loop.

Example:

Skip even elements

<code class="php"><?php
for ($i = 1; $i <= 10; $i++) {
    if ($i % 2 == 0) {
        continue;
    }

    echo $i . "\n";
}
?></code>

Output:

<code>1
3
5
7
9</code>

Perform different operations based on conditions

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

    echo $i;
}
?></code>

Output:

<code>1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz</code>

The above is the detailed content of How to use continue in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn