Loops in PHP – Explained

Loops in PHP allow us to repeat code until certain condition is met. A loop will go round and round and round multiple times executing the code until certain conditions are met. Very similar to the conditional statements but more powerful.


There are four PHP looping statements.

  • while – loops through a code block as long as the condition is met.
  • do…while – loop through a code block, then repeats only if the conditions are met.
  • for – loops through codes for defined numbers.
  • foreach – loops through code blocks for each item of an associative array.

Loops in PHP

Now let’s take a closer look in all these looping statements.

While Loop

While loop is the simplest of all looping statements. It simply runs till the given condition is true. Whatever condition we put between the parentheses of while loop, it evaluates into a Boolean value, that is true or false. If the condition becomes false, while loop will stop executing the code block.

The basic while loop syntax :

This is not a valid PHP code. It just demonstrates the while loop structure.

Let’s check an example to understand it clearly.

Here, we set a variable $i with value 1. While loop will keep executing the code until $i is less than or equal to 10. $i++ indicates that value of $i will increase every time the loop runs.

The condition has to return false at some point, otherwise the loop will never terminate.

Do….while Loop

Do….while loop executes a block of code and then evaluates the specified condition. If the condition is met, it will run through the code again and repeat the process.

The basic syntax of while loop :

Let’s check out the previous example with do…while loop.

Here, $i will be increased by one and then it will check the condition. As long as $i is less than or equal to 10, the loop will continue to run.

Unlike while loops, here the code is executed first and condition is checked later. This means, the code will executed at least once even if the condition evaluates to be false.

For Loops

For loop is a definite loop. That means you know how many times you want to execute the code block that you want to loop through. The syntax might seem a bit complex to you but once you break it down, you’ll see it’s really easy.

The basic syntax looks like this :

If you’re still confused , let’s check out an example to be more clear.

There are three expressions inside the parentheses.

First one initializes a loop counter. Generally, a variable is declared here. Second one evaluates if the condition is true or false, if it returns true, the loop continues. Otherwise the loop terminates. And finally third one is increment expression which increases the loop counter value.

So, here this example will print number 0 to 10.

Foreach Loops

Foreach loop is used to loop through any array. For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

Foreach loops works in two types of syntax. Let’s check them out.

Another one is :

The first syntax form loops over the$array. Every loop the value of the current element is assigned to $value. The second syntax will assign the current element’s key to the $key variable on each loop.

Let’s check out examples to be more clear.

Now let’s check out foreach loop for an associative array.

This will output the following :

Those are the PHP Loops you’ll need when you start writing PHP code.

Over to You

I hope now you’re clear about loops in PHP. If you have any confusions, feel free to ask me questions through comment section. Also don’t forget to share this with others if you find this useful.

Sharing is Caring
Imtiaz Rayhan

WordPress Enthusiast, Novice PHP Developer. Music Addict, The Beatles Fan. Conncect with me on Facebook, Google+ and Twitter.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: