Home  >  Article  >  Backend Development  >  PHP basic process control statements continue and break usage

PHP basic process control statements continue and break usage

伊谢尔伦
伊谢尔伦Original
2017-06-22 13:19:191729browse

continue
continue The loop structure is used to skip the remaining code in this loop and start executing the next loop.

Note: Note that in php tutorial the switch statement is considered a loop structure for continue purposes.

continue accepts an optional numeric parameter to determine how many loops to skip to the end of the loop.

<?php 
while (list($key,$value) = each($arr)) 
{ 
    if ($key == "zhoz")
    { 
    // 如果查询到对象的值等于zhoz,这条记录就不会显示出来了。 
    continue; 
    } 
do_something ($value); 
}

Example 2

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

break
break ends the execution of the current for, foreach, while, do..while or switch structure.

break can accept an optional numeric parameter to determine how many loops to break out of.

<?php
$arr = array (&#39;one&#39;, &#39;two&#39;, &#39;three&#39;, &#39;four&#39;, &#39;stop&#39;, &#39;five&#39;);
while (list (, $val) = each ($arr)) {
   if ($val == &#39;stop&#39;) {
       break;    /* you could also write &#39;break 1;&#39; here. */
   }
   echo "$val<br>n";
}
/* using the optional argument. */
$i = 0;
while (++$i) {
   switch ($i) {
   case 5:
       echo "at 5<br>n";
       break 1;  /* exit only the switch. */
   case 10:
       echo "at 10; quitting<br>n";
       break 2;  /* exit the switch and the while. */
   default:
       break;
   }
}
?>

Example 2

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

The above is the detailed content of PHP basic process control statements continue and break usage. 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