Home >Backend Development >PHP Tutorial >PHP: Example analysis of how Break and Continue break out of the loop

PHP: Example analysis of how Break and Continue break out of the loop

黄舟
黄舟Original
2017-06-25 09:45:401317browse

3f1c4e4b6b16bbbd69b2ee476dc4f83aec(2);2cacc6d41bbb37262a98f745aa00fbf0

Sometimes you may want to let the unconditional loop begin and allow the statements within the brackets to decide when Exit the loop.

There are two special statements that can be used in loops: break and continue.

break statement terminates or For loop while continuing to execute the current code after the following loop (if any). Alternatively,

you can put a number after the discounted keyword to describe how to get rid of multiple levels of the loop structure. This way, deeply nested loops buried

in one statement can break the outermost loop.

<?php
echo "<p><b>Example of using the Break statement:</b></p>";
for ($i=0; $i<=10; $i ) { 
   if ($i==3){break;} 
   echo "The number is ".$i;
   echo "<br />"; 
}
echo "<p><b>One more example of using the Break statement:</b><p>";
$i = 0;
$j = 0;
while ($i < 10) {
  while ($j < 10) {
    if ($j == 5) {break 2;} // breaks out of two while loops教程
    $j ;
  }
  $i ;
}
echo "The first number is ".$i."<br />";
echo "The second number is ".$j."<br />";
?>

continueThe statement terminates the execution of a or For loop in the statement block and continues the execution of the next iteration loop

<?php
echo "<p><b>Example of using the Continue statement:</b><p>";
for ($i=0; $i<=10; $i ) { 
   if (i==3){continue;} 
   echo "The number is ".$i; 
   echo "<br />"; 
} 
?>
</td> </tr> </table>

The above is the detailed content of PHP: Example analysis of how Break and Continue break out of the loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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