for
, while
or foreach
I always use break;
within a if
with the following structure:
$teste = array(1,2,3,4,5);
foreach($teste as $t){
if($t == 3){
echo "Terminando loop ao achar o número ".$t;
break;
}
echo $t."<br />";
}
echo "fim de script";
We can use continue:
foreach($teste as $t){
if($t == 3){
echo "O ".$t." é meu!";
continue;
}
echo $t."<br />";
}
echo "fim de script";
In the manual, you have:
Note that in PHP the switch is considered a loop structure for purposes.
But, does your want switch
instead of if
, and stop the foreach
structure when a particular occurrence is found?
Is it possible to use continue
for some purposes or break
to break the foreach
loop by using switch
?