PHP Loops

PHP loops are used to execute a block of code repeatedly for a fixed number of times or until a condition is met.

PHP for Loop

The for loop is used to run a block of code repeatedly given that the number of iterations or the number of times the loop should run is known.

The for loop contains three parameters:

  • Initialization: This is the initialization of the loop counter. For example, $i = 0 which means that our loop counter I should start from 0. It can be set to any value.
  • Test Expression: This expression tests a condition, if the condition is true, it runs the block of code repeatedly and moves to update, if the condition is false, the for loop stops. For example, $i <= 5.
  • Update Expression: This expression updates the loop counter after executing the block of code inside the for loop. It is often used to either increment or decrement the loop counter, it depends on its use case. For example, $i++ increments the loop counter by one.

PHP for Loops Example

  Do It Yourself

The above example prints out the value of the loop until the condition in the test is false.

PHP foreach Loop

The foreach loop is used to loop through key/value pairs of an array.

Unlike the for loop, the foreach loop does not need any condition to be met.

foreach($array as $value) {

In the syntax above, the foreach loop traverses the array stored in the $array variable. On each loop, the current element is assigned to the $value variable.

foreach($array as $key=>$value) {

In the syntax above, the foreach loop gives you access to the key of the current element and its corresponding value storing the former in the $key variable and the latter in the $value variable. This approach is mostly used when traversing through an associative array.

The example below will traverse an array of animals and print out each individual animal.

PHP foreach Loop Example

 tag adds a new line
         foreach($animals as  $animal) {
                 echo "
$animal "; } ?>
  Do It Yourself

PHP while Loop

The while loop runs a block of code repeatedly as long as the specified condition is met.

It runs the block of code repeatedly until the condition is false and will only run if it is true from the very beginning.

The example below will run print out I Love PHP! repeatedly until $i is greater than 5 at the same time incrementing $i.

PHP while Loop Example


             //then increment $i or else the loop will run non-stop and might end up crashing your PC.

  Do It Yourself

The PHP while loop can also be written by using the alternate syntax:

while (expression):



PHP break/continue

In PHP or most programming languages in general, break and continue are not really loops but gives you control over loops. It lets you to either break from a loop or skip an iteration.

The break statement is used to jump or break out of a loop and can be used within a  conditional statement. It gives you the control of jumping out of a loop when a certain condition is met. for example, you might have a for that loops through an array and possibly want to break out of the loop when it gets to the third element in the array.



  Do It Yourself

The continue statement is used to skip an iteration in the loop and can also be used within a conditional statement. The example below loops through an array of animals and prints each individual animal but skips the value goat if it is present in the array.


  Do It Yourself

What You Should Know at The End of This Lesson

  • You should know about PHP loops
  • You should know how to traverse an array using the for, foreach and while loop
  • You should know about the break and continue statement