filmov
tv
PHP break vs. continue statement

Показать описание
Both break and continue statements are used with loop or iteration process.
Keyword break is used to quit a loop instantly.
Keyword continue is used to skip a particular iteration.
Let's explain with examples.
Start with break statement.
In this while loop when value of i will be 5, there is a break, then it exit the loop, and rest of the iterations will be exited forever. Results: 1, 2, 3, 4.
$i = 0;
while ($i lt;= 10)
{
$i++;
if ($i == 5)
{
break;
}
echo $i . "\n";
}
Now, update the same example and replaced break with continue.
When value of i becomes 5, condition satisfies and continue statement executed. As a result, iteration skip, 5 is not printed, next iterations will be continued. Results: 1, 2, 3, 4, 6, 7, 8, 9, 10.
$i = 0;
while ($i lt'= 10)
{
$i++;
if ($i == 5)
{
continue;
}
echo $i . "\n";
}
Keyword break is used to quit a loop instantly.
Keyword continue is used to skip a particular iteration.
Let's explain with examples.
Start with break statement.
In this while loop when value of i will be 5, there is a break, then it exit the loop, and rest of the iterations will be exited forever. Results: 1, 2, 3, 4.
$i = 0;
while ($i lt;= 10)
{
$i++;
if ($i == 5)
{
break;
}
echo $i . "\n";
}
Now, update the same example and replaced break with continue.
When value of i becomes 5, condition satisfies and continue statement executed. As a result, iteration skip, 5 is not printed, next iterations will be continued. Results: 1, 2, 3, 4, 6, 7, 8, 9, 10.
$i = 0;
while ($i lt'= 10)
{
$i++;
if ($i == 5)
{
continue;
}
echo $i . "\n";
}