PHP Break & Continue Statement
ADVERTISEMENTS
PHP Break Statement
PHP Break statement is used for stopping iteration of loops. PHP Break statements used with the following loops:
- While loop
- Do While loop
- For loop
- Foreach loop
- Switch Statement
Understand break statement from these examples :
Example 1:
<?php
$array_days = array("It's Sunday", "It's Monday", "It's Tuesday", "It's Wednesday", "It's Thursday", "It's Friday", "It's Saturday", );
foreach ($array_days as $day)
{
if ($day== "It's Thursday")
{
break;
}
echo $day . "\n";
}
?>
Output:
It's Sunday
It's Monday
It's Tuesday
It's Wednesday
Example 2
<?php
$m = 0;
while (++$m)
{
switch ($m)
{
case 2:
echo "statement 5:\n";
break 1;
case 4:
echo "statement 10:\n";
break 2;
default:
echo "Error!\n";
break;
}
echo "\n";
}
?>
Output:
Error!
statement 5:
Error!
statement 10:
PHP Continue Statement
-
This statement is used within looping structures to skip the rest of the current loop iteration.
-
And This continues execution at the condition evaluation then the beginning of the next iteration.
Understand continue statement from this example :
<?php
for ($i = 0; $i < 5; $i++)
{
if ($i == 2)
continue;
echo $i . "\n";
}
?>
Output:
0
1
3
4