Friday, August 25, 2017

PHP Break Statement


PHP break statement breaks the execution of current for, while, do-while, switch and for-each loop. If you use break inside inner loop, it breaks the execution of inner loop only.

Syntax: 
statement;  
break;

Example :1 
lets see the below example of break statement and understand the control flow.
<?php  
for($i=1;$i<=10;$i++){  
echo "$i <br/>";  
if($i==7){  
break;  
}  
}  
?>

Output:
----------------------








Example : 2
lets see the example of break statement with switch statement.
<?php  
$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;
    }
}
?>

Output:
----------------
At 5
At 10; quitting




No comments:

Post a Comment