Ejecutando break / continue en for, foreach o while php
En este ejemplo mostrare como detener / continuar la Ejecución de un for, foreach o while por medio del break o continue en php.
aquí el código:
Dentro de cualquier ejecución el break / continue se ejecutara sin problema.
aquí el código:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Break or continue</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<section>
<?php
//Break Continue in for bueno!!!
for($a=0; $a<=10;$a++)
{
echo "Iteracion de $a<br>";
if($a==2){
continue;
}
if($a==5){
break;
}
}
//continue in while
$b=2;
while($b<7){
$b++;
switch($b){
case 4:
continue 2;
case 6:
continue 2;
}
echo "$b<br>";
}
//Break Continue in foreach !!!
$colores=["Verde","Rojo","Oranje"];
foreach($colores as $valor)
{
if($valor=="Oranje")
{
continue;
}
switch($valor){
case "Verde":
break;
}
echo "$Valor <br>";
}
?>
</section>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Break or continue</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<section>
<?php
//Break Continue in for bueno!!!
for($a=0; $a<=10;$a++)
{
echo "Iteracion de $a<br>";
if($a==2){
continue;
}
if($a==5){
break;
}
}
//continue in while
$b=2;
while($b<7){
$b++;
switch($b){
case 4:
continue 2;
case 6:
continue 2;
}
echo "$b<br>";
}
//Break Continue in foreach !!!
$colores=["Verde","Rojo","Oranje"];
foreach($colores as $valor)
{
if($valor=="Oranje")
{
continue;
}
switch($valor){
case "Verde":
break;
}
echo "$Valor <br>";
}
?>
</section>
</body>
</html>
Comentarios
Publicar un comentario