php continue statement

continue statement is used continues executing the code that follows after the loop.

Continue Syntax

<?php
for(loop)
{
   if (condition)
   {
   continue;
   }
}
?>

Continue Example


<?php
$counter = -4;
for ( ; $counter <= 10; $counter++ ) {
if ( $counter == 0 ) {
continue;
}
$temp = 4000/$counter;
print "4000 divided by $counter is... $temp<br>";
}
?>

Out put :

4000 divided by -4 is... -1000
4000 divided by -3 is... -1333.33333333
4000 divided by -2 is... -2000
4000 divided by -1 is... -4000
4000 divided by 1 is... 4000
4000 divided by 2 is... 2000
4000 divided by 3 is... 1333.33333333
4000 divided by 4 is... 1000
4000 divided by 5 is... 800
4000 divided by 6 is... 666.666666667
4000 divided by 7 is... 571.428571429
4000 divided by 8 is... 500
4000 divided by 9 is... 444.444444444

In the above example We use an if statement to test the value of $counter. If the $counter variable is equivalent to zero, the iteration is skipped, and the next one starts immediately.

The break and continue statements can make code more difficult to read. Because they often add layers of complexity to the logic of the loop statements that contain them, you should use them with care.





Content