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.
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:
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!