This article brings you the content is about the PHP in the use of foreach () If you want to jump out of the loop or terminate the loop implementation method, there is a certain reference value, the need for friends can refer to, I hope to help you.
Example one:
In PHP, in a foreach () loop, you want to be in a loop, when a condition is met,
$arr = Array (' A ', ' B ', ' C ', ' d ', ' e '); $html = '; foreach ($arr as $key = = $value) { if ($value = = ' B ') { $html. = $ value; Continue When the $value is B, jump out of the loop } if ($value = = ' C ') { $html. = $value; Break When $value is C, terminates the Loop } $html. = $value;} Echo $html; Output: AB
To jump out of this loop to continue with the next loop, or to satisfy a condition, terminate the foreach () loop, which will use: Continue and break.
Example two:
<?php$array = Array (1,2,3,4,5,6,7,8,9), foreach ($array as $value) { echo $value; if ($value = = 5) {break ; }}?> results: 12345