
In this tutorial, we will be learning about break and continue statements.
What Are Break Statements?
To end the entire loop earlier if a particular condition is met, you can use the “break” keyword.
Example
<?php
for($i=0; $i<100; $i++)
{
if($i==2)
{
break;
}
echo $i;
}
?>
Result

The above code will only execute up until $i==2. What would have happened when $i==3, $i==4 etc is completely irrelevant.
In other words, it would not matter whether we had written instead $i<100, $i<10, $i<5 or $i<1000. The result is the same, since the loop had already been broken when $i==2.
What Are Continue Statements?
Break will end the entire loop. If you only want to end the current iteration and not the loop itself, you should use continue instead. Hence, if instead we wrote: Example
<?php
for($i=0; $i<100; $i++)
{
if($i==2)
{
continue;
}
echo $i;
}
?>
Result

The code for $i==2 would have stopped executing, and the code will move to the $i==3 iteration.
About the author
I'm a full-stack web developer who loves building new projects and learning new things
It might not show from my posts, but I'm also quite funny.
Comments
Join the discussionNo comments yet. Be the first to comment.




