The continue and Break statement

The continue statement:
The continue statement is a little like a break statement, except that it instructs PHP to stop processing the current loop and to move right to its next iteration. So, instead of breaking out of the whole loop.PHP exits only the current iteration. This approach can be useful in the case where you know there is no point continuing execution within the current loop and you want to save processor cycles or prevent an error from occurring by moving right along to the next iteration of the loop.

The break statement breaks the execution of the current loop i.e for, for each, while,do-while, switch statements.

1.Example for Continue statement:
<?php
for ($i = 0; $i < 5; ++$i) {
    if ($i == 2)
        continue
    print "$i\n";
}
?>  
OUTPUT:
0
1
3
4
2.Example for break Statement:

<?php  
for($i=1;$i<=10;$i++){  
echo "$i <br/>";  
if($i==5){  
break;  
}  
}  
?>  

Output:

1
2
3
4
5

Translate »