Key Concepts

Review core concepts you need to learn to master this subject

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.

Lesson 1 of 2
  1. 1
    When 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…
  2. 2
    The first PHP loop that we will cover is the while loop. This type of loop continues to iterate as long as its conditional is true. This code outputs the numbers from 1-10, similar to the previous…
  3. 3
    A do…while loop is very similar to a while loop. The main difference is that the code block will execute once without the conditional being checked. After the first iteration, it behaves the same…
  4. 4
    A for loop is commonly used to execute a code block a specific number of times. for (#expression 1; #expression 2; #expression 3) { # code block } The for loop syntax includes 3 expressions: - …
  5. 5
    The foreach loop is used for iterating over an array. The code block is executed for every element in the array and the value of that element is available for use in the code block. Our counting t…
  6. 6
    Similar 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 …
  7. 7
    Now you have the ability to repeat execution of code blocks as necessary in your PHP programs. Here is a summary of the topics covered in this lesson: - while loops execute only as long as their c…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo