Explain break and continue in php

 





use of break:

Normally break actually use jump out of a statement or a loop. let's see a example below:

<?php
for ($x = 0; $x < 5; $x++) {
  if ($x == 3) {
    break;
  }
  echo "$x <br>";
}
?>

this example result will be 0,1,2 cause when the value is equal to 3 it actually jump out of the loop.


use of continue:

Continue actually use for break or skip specific itearation and then continue the rest of the loop.  let's see a example below:

<?php
for ($x = 0; $x < 5; $x++) {
  if ($x == 3) {
    continue;
  }
  echo "$x <br>";
}
?>

this example result will be 0,1,2,4 cause when the value is equal to 3 it actually skip it and continue rest of the loop.


Post a Comment (0)
Previous Post Next Post