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

How to use continue in php

下次还敢
Release: 2024-04-29 10:36:13
Original
634 people have browsed it

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>
Copy after login

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>
Copy after login

Output:

<code>1
3
5
7
9</code>
Copy after login

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>
Copy after login

Output:

<code>1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz</code>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template