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

黄舟
Release: 2023-03-11 11:14:01
Original
1268 people have browsed it

<script>ec(2);</script>

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 />";
?>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!