PHP Loop

PHP Loop

PHP loops are used to execute some code again and again for some definite number of times. There are many types of looping statements in PHP. They are:

  • while loop
  • do…while loop
  • for loop
  • foreach loop

Let’s discuss each of these loops.

While loop

The while loop is used to execute the specified code as long as the condition is true. The syntax of the while loop is given below:

while (condition) code to be executed;

Let’s see a simple code to display numbers 1-10 by using while loop.

<?php $i=1; while (i<=10) {     echo $i."<br/>";       $i++; } ?>

This simple code displays the numbers 1-10 in separate lines.

1 2 3 4 5 6 7 8 9 10

Let’s see how the above code is executed.
First the value of “i” is initialised to 1. Then the condition is checked and since “i” is less than 10, the code within the while loop is executed and 1 will be displayed. Then the value of “i” will be incremented. and the condition will be again checked and the new value of “i”, i.e. 2 will be displayed. This will continue till the condition is true. Notice, we increment the value of “i” in the loop. You should have a statement in your while loop which will eventually cause the condition to be false. If you don’t do this, the loop will never stop and your program will appear “frozen”.

Do…while loop

A do…while statement is a modified version of while loop. The main difference between both these loops is that the while loop checks the condition before executing the code within it while do while loop executes the code once and then checks the condition. Do while loop is used when the code withing the loop has to be executed at least once irrespective of the condition. The syntax of do while loop is:

do {    code to be executed; } while (condition);

The following example will illustrate this:

<?php $i=1; do {     echo $i."<br/>";       $i++; } while ($i<=10); ?>

The output will be:

1 2 3 4 5 6 7 8 9 10

In the example above, the parser will initialize the value of “i” to 1 and then it will execute the code within the do while loop and then it will check the condition. So even if the condition is false, the code within the loop will be executed at least once.

For loop

The for loop is one of the most used loops because it is very logical and easy to use. The syntax of the for loop is:

for (initial; condition; increm) {      code to be executed; }

The syntax of for loop has three parts:

  • initial:Initialize the counter.
  • condition:The condition to be checked at the beginning of each loop. If the condition is true the code within the loop will be executed and if it is false, it will not be executed.
  • increm:Increment or decrement the counter.

Note that each part of the for loop is separated by semicolon. Below is a simple example:

<?php for ($i=1; $i<=10; $i++) echo $i."<br/>"; ?>

and the output will be:

1 2 3 4 5 6 7 8 9 10

Foreach loop

The foreach statement is used to loop through arrays. For the current value of the array, the code within the loop is executed. The syntax of the foreach loop is:

foreach (arrayname as value) {      code to be executed; }

Let’s see a simple example:

<?php $arr=array(1,2,3,4,5,6,7,8,9,10); foreach ($arr as $v) echo $v."<br/>"; ?>

The output will be:

1 2 3 4 5 6 7 8 9 10
If you have found my website useful, please consider buying me a coffee below 😉