BREAK



ESTRUCTURAS DE CONTROL


BREAK


La opción break termina la ejecución de la estructura actual for, foreach, while, do-while o switch.

break acepta un argumento numérico opcional el cual indica de cuantas estructuras anidadas encerradas se debe salir.


Ejemplo 1:
---------------------------------------------------------------
<?php
$arr = array('uno''dos''tres''cuatro''pare''cinco');
while (list(, 
$val) = each($arr)) {
    if (
$val == 'pare') {
        break;    
/* Se puede también escribir 'break 1;' aquí. */
    
}
    echo 
"$val<br />\n";
?>
---------------------------------------------------------------

   

Ejemplo 2: Trate de repasar el funcionamiento con el siguiente código
-----------------------------------------------------------------------------
<?php
/* Usando el argumento opcional. */$i 0;
while (++
$i) {
    switch (
$i) {
    case 
5:
        echo 
"En 5<br />\n";
        break 
1;  /* Sólo sale del switch. */
    
case 10:
        echo 
"En 10; saliendo<br />\n";
        break 
2;  /* Sale del switch y del while. */
    
default:
        break;
    }
}


?>
---------------------------------------------------------------

->===

->===

0 comentarios:

Oferta





Launch your Online Business with a $5.99 .COM!