Loops in PHP| While in PHP| do while PHP| foreach in PHP

Control Statements in PHP| If Else in PHP | Nested If Else in PHP | Switch in PHP

What is a loop?

A loop is a programming structure that repeats itself up to a fixed number or until a given condition is met. In PHP, we apply a loop statement so that we can get multiple values at a single time. Suppose a user wants to retrieve values up to 100. Commonly, it is impossible to define 100 variables and get the result. But here loop can solve the issue. So, we will discuss it one by one.

Types of a loop:

  • PHP for loop
  • PHP while loop
  • PHP do-while
  • PHP foreach
  • PHP break

PHP for loop

For loop is used when a user knows how many times, he wants to run a code

Syntax of the code:

for(initialization; condition; increment/decrement){ 
//code to be executed 
}

For loop has 3 parameters. Given are:

  • Initialization: initialization is a value from where a user wants to start its loop. It is only defined once. i.e. $a = 1;
  • Condition: loop continues until the given condition becomes false. If condition is true, it will keep on repeating. i.e. $a<=10;
  • increment/decrement: it increases or decreases the value i.e. $a++

Check this example:

<?php for ($a = 1; $a <= 10; $a++) {
    echo "$a<br/>";
} ?>

The output will be:

1 
2 
3 
4 
5 
6 
7 
8 
9 
10

Working of for loop:

  • Step 1 starts from the initialization of variable a from number 1. Then it goes to the 2nd parameter i.e. condition to check if 1 is less than 10. If it is true, it goes directly to echo statement to print 1.
  • After printing 1, it goes to increment parameter and increase the current number 1 by 1 and makes it 2 then again it goes back to its condition to check if 2 is less than 10. If it is, then it goes back down to echo statement to print 2.
  • It will again go back to increment and increase it to 3.
  • This cycle will keep repeating until the increment parameter makes a variable value to 11. The condition will be checked. Here 11 will not be less than 10. Hence the program will terminate at this point and the user will get values up to 10. So, this is how a simple for loop statement works.

PHP nested for loop

Nested for loop means: for loop inside for loop. You can also say that it returns values in form of rows and columns.

Check this example:

<?php for ($i = 1; $i <= 2; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        echo "$i $j<br/>";
    }
} ?>

The output will be:

1 1 
1 2 
1 3 
2 1 
2 2 
2 3

Working of nested for loop:

  • Here 2 variables are taken. The outer loop will be created in rows. The inner loop will be created in columns. The outer loop is taken as variable ‘i’ and the inner loop is taken as variable ‘j’
  • In step 1 outer loop will initialize from i = 1. Then it will go to the outer loop condition to check if 1 is less than 2. If yes then it will go inside for loop and initialize inner loop from variable j = 1. Now again condition will be checked in the inner loop. If the inner loop variable ‘j’ equals to or less than 3, then it will go inside the inner loop echo statement and print both variable values i.e. variable ‘i’ = 1 and ‘j’ = 1. Then it will go to the next line because of the tag
  • Now, it will go back to the inner loop increment parameter and increase the variable ‘j’ value to 2. It will go back to the inner loop condition to check if 2 is less than or equals 3. It is true, it will go to echo statement and print number 2.
  • Again, it will go to the inner loop increment parameter and increase the number to 3, and will check the number if it is equals to or less than 3.
  • The cycle will keep on repeating until the inner loop number becomes not less than or equals 3 i.e. the time it becomes 4, the inner loop will terminate and go back to outer loop increment.
  • At this point, the outer loop becomes 2 because till now it was 1. So, the condition will be checked at the outer loop condition if 2 is less than or equals 2. If it is, then it will go inside the inner loop and repeat all its steps. And when the outer loop becomes greater than 2 the outer loop will terminate and the whole program will complete its step and will give its output.
  • Hence, the inner loop will run 3 times and the outer loop will 2 times.

PHP while loop

We have discussed earlier that for loop is applied if you know how many times you want to run your loop. But what if you do not know the ending number where you want to end your loop. In such cases, we use a while loop. In a while loop, we use a while loop to repeat a set of statements when several iterations are not known before its execution.

Syntax of while loop

while(condition) { 
// Body of while loop 
}

Parts of while loop

Unlike for loop, while does not contain initialization and update part. It contains only two parts – condition and body of the loop.

Print this program

Print numbers from 1 to 10 using while loop.

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

Working of while loop:

  • First, a variable is initialized from 1.
  • Then this number will reach inside while condition to check if 1 is equal to or less than 10. If it is then it will go to echo statement to print 1.
  • Then 1 will increase by 1 number and will make 2 then this number will go back to condition to check if 2 is less than or equals to 10 and will go back to echo statement.
  • This cycle will again keep repeating till the program terminate itself.

Output will be

1
2
3
4
5
6
7
8
9
10

PHP Nested While Loop

We can use a while loop inside another while loop in PHP, it is known as nested while loop.

Check this example:

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

Output will be

1 1 
1 2 
1 3 
2 1 
2 2 
2 3 
3 1 
3 2 
3 3

Working of the nested while loop

  • The initialization of the loop will start from ( i = 1 ). Then it will go to the while statement to check 1 is less than or equals 3. If it is, then the program will move to the body of the loop i.e. to another while loop.
  • Here initialisation will start from ( j = 1 ). Then the program will move down to the condition of the inside loop i.e. ($j <= 3) to check if 1 is less than or equals 3. If it is, then it will move further to echo statement to print both ‘i’ variable and ‘j’ variable.
  • In the next step, the current value of j will increase by 1 because of ( $j++ ) and make it to 2. Then this step will again go to check the condition of the inner loop if j = 2 is less than or equals 3.
  • If it is, then it will again come inside of the body of the inner loop till $j becomes equal to 3. At this point, $j will terminate its loop and will go to $i++. Here value of $i will increase to 2. This will go to outer loop condition i.e. ( $i<=3 ). And will again inside till the whole process terminate at one point.

Loops in PHP| While in PHP| do while PHP| foreach in PHP
Show Buttons
Hide Buttons