How to break out of loop in php? Introduction to the use of pop-up functions in php

伊谢尔伦
Release: 2023-03-11 06:36:02
Original
2878 people have browsed it

1.break jumps out of the code fragment and ends the loop.

The break statement terminates or For loop at the same time, continues 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

2.continueEnd the current segment, end this cycle, and continue the next cycle
3.exit End the entire PHP code

The function of break is to jump out of this loop (if this break or continue is in the if statement in the loop, it does not jump out of the if statement, but jumps out of the loop statement). After executing the curly braces of this loop The statement,
break is like this in the loop statement, the switch statement is also like this with the function, and continue means that when the conditions are met, the statement after the loop will not be executed, starting from the beginning of the loop Re-run.

<?php  
/* php的break,continue,return 的简单区别代码 */  
$i = 1;    
while (true) { // 这里看上去这个循环会一直执行    
    if ($i==2) {// 2跳过不显示    
        $i++;    
        continue;    
    } else if ($i==5) {// 但到这里$i=5就跳出循循环了    
        break;    
    } else {    
        echo $i . &#39;<br>&#39;;    
    }    
    $i++;    
}    
exit;    
      
echo &#39;这里不输出&#39;;
?>
Copy after login

break is used to break out of the currently executing loop and no longer continue to execute the loop.

<?php 
$i = 0; 
while ($i < 7) { 
if ($arr[$i] == "stop") { 
break; 
} 
$i++; 
} 
?>
Copy after login

continue Immediately stops the current execution loop and returns to the conditional judgment of the loop to continue the next loop.

<?php 
while (list($key,$value) = each($arr)) { 
if ($key == "zhoz"){ // 如果查询到对象的值等于zhoz,这条记录就不会显示出来了。 
continue; 
} 
do_something ($value); 
} 
//  例子2 
foreach ($list as $temp) { 
if ($temp->value == "zhoz") { 
continue; // 如果查询到对象的值等于zhoz,这条记录就不会显示出来了。 
} 
do_list; // 这里显示数组中的记录 
} 
?>
Copy after login

Note: The goto loop instruction cannot be used in PHP.

The above is the detailed content of How to break out of loop in php? Introduction to the use of pop-up functions in php. 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!