Loops in PHP
Loops are a key way to control execution of code. Learn how to use them effectively in PHP.
StartKey Concepts
Review core concepts you need to learn to master this subject
PHP break keyword
PHP do while loops
PHP for loop
PHP while loops
PHP foreach loop
PHP continue keyword
PHP while loop shorthand
PHP for loop shorthand
PHP break keyword
PHP break keyword
// We can use the break statement to end the loop once the count reaches 4
$count = 1;
while ($count < 10)
{
echo "The count is: " . $count . "\n";
if ($count === 4) {
break;
}
$count += 1;
}
In PHP, break
can be used to terminate execution of a for
, foreach
, while
or do…while
loop.
One downside of heavy usage of break statements is that code can become less readable.
Loops
Lesson 1 of 2
- 1When attempting to repeat code over and over again, it can be monotonous to retype or copy and paste the same code. Worse, inadvertent typos can cause errors in your program. Consider an example w…
- 6Similar to switch statements, the break keyword can be used to terminate any of the loop types early. In our counting example of a while loop, if we add a conditional and a break statement: $count …
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory