break
break
will end the execution inside the block of following statements:
for, foreach, while, do-while and switch structure
Syntax:
1 |
break [numeric number]; |
numeric number |
default value is 1, which tells to PHP processor that how many nested enclosing structures are to be skipped. |
Syntax Example:
1 |
break; break 1; break 2; break 5; |
Where both break and break 1 are equivalent.
Example explanation:
break |
end the execution in the current block{ -> end this block
|
break 2 |
end the execution in the 1st outer of current block{ -> end this block
|
break 3 |
end the execution in the 2nd outer of current block{ -> end this block
|
Consider the following example which contain while loop and switch structure.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $i = 1; echo "loop started at " . $i . "<br />"; while($i++) { switch($i) { case 2; echo "break 1 used inside switch when i = " . $i . "<br />"; break; case 4: echo "break 2 used inside switch when i = " . $i . ". This will end the while loop process, so 'i = $i, inside while loop' will not be printed<br />"; break 2; } echo"i = $i, inside while loop <br />"; } ?> |
- We just iterate the
while
loop, thewhile($i++)
is a endless process and it always return true. - To terminate the loop process, we have used
break
andbreak 2
. - Inside
case 2
, we have usedbreak
, which will end the execution in the current block, ie)switch
block. - Inside
case 4
, we have usedbreak 2
, which will end the execution in the outer block, ie)while
loop block and so the process ended at this point.
More Examples:
Example for break with foreach loop
Example for break with for loop
continue
continue
will stop the execution at the point and continue the remaining looping process inside the block of following statements:
for, foreach, while, do-while and switch structure
Syntax:
1 |
continue [numeric number]; |
numeric number |
default value is 1, which tells to PHP processor that how many levels of enclosing loops it should skip to continue for remaining iteration. |
Consider the following example which contain while loop, for loop and if statement.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php $i = 0; echo "loop started at " . $i . "<br />"; while($i < 5) { $i++; for($j = 1; $j <= 2; $j++) { if($i == 2) { echo "continue 1 used inside if statement when i = " . $i . " and j = $j<br />"; continue 1; // at this point, the process is stopped here and will continue remaining iteration of for loop } else if($i == 4) { echo "continue 2 used inside if statement when i = " . $i . " and j = $j. " . "This will continue the while loop process, " . "so both (i = $i, inside while loop) and " . "(inside for loop. i = $i and j = $j) will not be printed" . "<br />"; continue 2; // at this point, the process is stopped here and will continue remaining iteration of while loop } echo "inside for loop. i = $i and j = $j <br />"; } echo"i = $i, inside while loop <br />"; } ?> |
- We have used
continue 1;
insideif($i == 2)
this will stop the execution at this place/point and continuefor
loop iteration. Soecho "inside for loop. i = $i and j = $j <br />";
is not executed. - We have used
continue 2;
insideelse if($i == 4)
this will stop the execution at this place/point and continuewhile
loop iteration. So bothecho "inside for loop. i = $i and j = $j <br />";
andecho"i = $i, inside while loop <br />";
is not executed inside while of $i = 4 block and continue while of $i = 5 block.
Please follow and like us: