Php break
From w3cyberlearnings
Contents |
break
break break ends execution of the current for, foreach, while, do-while or switch structure. It is just mean STOP.
Syntax
foreach($arr as $aVal) { statement break; } while(expression) { if(express1==expression2) { break; } } while (express) { switch (expres) { case condition1: statement break; case condition2: statement2; break; default: break; } }
Example 1: Break within the for loop
- When $i=3, STOP
<?php $count = 5; for ($i = $count; $i > 0; $i--) { if ($i == 3) { break; } echo $i . "<br/>"; } ?>
Output
5 4
Example 2
<?php $t = 10; while ($t-- > 0) { switch ($t) { case 1: echo $t; echo '<br/>'; break; case 2: echo $t; echo '<br/>'; echo '2 NO break'; case 3: echo $t; echo '<br/>'; echo '3 No break'; default: echo 'default'; echo '<br/>'; break; } } ?>
Output
default default default default default default 3 3 No breakdefault 2 2 NO break2 3 No breakdefault 1 default