break is a reserved word in some computer programming languages, and its function is to terminate the loop at the current level in most cases. In the switch (switch statement) of C language, the break statement can also be used to jump out of the current switch structure immediately after executing a case (branch). In the process of debugging some programs, break is used to set breakpoints. Next we will introduce the role of break in PHP.
Recommended tutorial: PHP video tutorial
##break ends the current for, foreach, while, do- Execution of while or switch structure.
break can accept an optional numeric parameter to determine how many loops to break out of.
<?php $arr = array('one', 'two', 'three', 'four', 'stop', 'five'); while (list (, $val) = each($arr)) { if ($val == 'stop') { break; /* You could also write 'break 1;' 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; } } ?>
The above is the detailed content of The role of break in php. For more information, please follow other related articles on the PHP Chinese website!