PHP Continue

When executed, continue skips the rest of the code inside the current iteration of the loop and jumps to the next iteration.


Using continue in a for Loop


The continue statement stops the current iteration in the for loop and continue with the next.


Example
<!DOCTYPE html>
<html>
<body>
<?php
for ($i = 0; $i < 10; $i++) {
if ($i % 2 == 0) {
continue; // Skip the rest of the loop for even numbers
}
echo "Odd number: $i\n";
}
?>
</body>
</html>

Try it yourself

Using continue in a while Loop


The continue statement stops the current iteration in the while loop and continue with the next.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$i = 0;
while ($i < 10) {
$i++;

if ($i % 2 == 0) {
continue; // Skip the rest of the loop for even numbers
}

echo "Odd number: $i\n";
}
?>
</body>
</html>

Try it yourself

Using continue in a do...while Loop


The continue statement stops the current iteration in the do...while loop and continue with the next.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$i = 0;
do {
$i++;

if ($i % 2 == 0) {
continue; // Skip the rest of the loop for even numbers
}

echo "Odd number: $i\n";
} while ($i < 10);
?>
</body>
</html>

Try it yourself

Using continue in a foreach Loop


The continue statement stops the current iteration in the foreach loop and continue with the next.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = [1, 2, 3, 4, 5, 6];

foreach ($numbers as $number) {
if ($number % 2 == 0) {
continue; // Skip the iteration if the number is even
}

echo "Odd number: $number\n";
}
?>
</body>
</html>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.