Check out the Latest Articles:

Hope you are well versed with conditional statements in PHP. This time we are going to see how to work with looping structures. Looping structure are basically those statements which are to be executed recursively until some condition is fulfilled.

PHP provides 3 types of looping structures

  • While Loop
  • Do While Loop
  • For Loop

While Loop

While loops are the simplest form of looping structures in PHP. If you have already worked with C then these resemble same as the while loop in C. The basic syntax is

while (condition) {
… Statement to be executed
… Statement to be executed
}

The above code will check for the condition and keeps executing the statements inside the loop or block until the condition is returned false. Let us see this with an example.

<?php
// lets try to echo numbers 0 to 10
$a = 0;
while ($a <= 10) {
echo $a;
$a++;
}
?>

The above code is pretty self explanatory, we took a variable $a with value 0. The condition is to check if $a is equal or less then 10, Then print the value of a on the screen. Increment the value of a by 1. Check the condition again and so on until the value of a is 10. and when it exceeds 10, the loop is terminated.

6-while-loop-1

Do While

The Do While loop is almost similar to while loop. The only thing it differs from a simple while loop is that here the statements are executed first and then the condition is checked. The syntax is

do {
… statement 1
… statement 2
}  while (condition) ;

Confused? Let us see an example

<?php
$i = 0;
do {
echo ++$i;
} while ($i < 10);
?>

Here we will see that the number from 1 to 10 are printed even though our condition states that $i < 10. This is because when the value of $i is 9 the condition is true and since the statement gets printed first, the value of $i is printed 10 and then the loop gets terminated as per the condition. See the image and get an idea

6-while-loop-2

For Loop

The for loop is the most complex loop in PHP. Behavior is same like the for loop in C. Let see the syntax before explaining it.

for (expr1; expr2; expr3) {
statement 1;
statement 2;
}

If you take a look, the expr1 is the expression that is executed at the starting of the loop for once. Then the statements are executed, and the condition is checked after each iteration. The condition is stated in expr2. The another thing that is added here is the expr3. The expr3 is a statement that will be executed after each iteration. Looks like I’m confusing you once again. Let’s see an example

<?php
for ($i = 0; $i < 10; $i++) {
echo $i;
}
?>

I hope the example makes you clear about the topic. Here expr1 is executed first and $i is initialized. The next thing is the echo line is executed. The iteration is over, now the condition is checked and if the condition is true then expr3 i.e $i++ is executed and it goes into the loop once again. The output

6-for-loop-1

Related posts:

  1. Using Conditional Statements in PHP – Learn PHP in 7 Days – Day #4
  2. Learn PHP in 7 Days
  3. PHP Syntax, Variables, Echo Statement – Learn PHP in 7 Days – Day #1
  4. Using Operators in PHP – Learn PHP in 7 Days – Day #2
  5. Using Comments in PHP – Learn PHP in 7 Days – Day #3


  1. It‘s quite in here! Why not leave a response?