PHP Loops

PHP loops are similar to other loops in other programming languages like C, C++, Python, etc. Loops are an essential part of any programming or scripting language.

PHP loops help in the execution of a block of code multiple times by writing it only once. All loops work around a given condition. The programmer gives a condition to run the loop. Each time the loop runs, the compiler checks the given condition; if it evaluates to true, then the loop runs, otherwise not.

Where are PHP loops used?

Loops are used when there is a possibility of repetitive statements, like printing a star pattern or printing numbers from 1 to 10. Instead of writing the print/echo statement multiple times, we can automate the process using loops.

PHP supports four different types of loops:

  • For Loop
  • Foreach Loop
  • While Loop
  • Do-while Loop

PHP For Loop

PHP For Loop is ideal to use when you know exactly how many times the Loop should execute. For Loop has three parameters: initialization, condition and increment/decrement.

The parameters of for loop have the following meanings:

  • Initialization: Initialization means to assign a value to a variable. This variable will be used as a counter in For Loop. This counter variable's value will be incremented or decremented according to the problem.
  • Condition: This is the test condition. The compiler checks the condition, and if it evaluates to true, only then the Loop will execute.
  • Increment/decrement: It updates the counter variable's value. At the end of each iteration(end of one Loop), the condition is checked, and the counter variable's value gets incremented/decremented.
Syntax
for(initialization; condition; increment/decrement)
{
// code to be executed
}

Let's see an example below. The example below defines a loop that starts with $i=1. The loop will execute until $i is less than or equal to 3. The variable $i will be incremented by 1 each time after one complete execution of Loop.

Example

<?php for($i=1; $i<=3; $i++){ echo "Number: " . $i . "<br>"; } ?>

Output

Number: 1
Number: 2
Number: 3

PHP Nested For Loop

Nested for Loop is a 'For loop inside another For loop'. Nested loops can be programmed in any programming language which has Loop. The different star pattern programs are examples of Nested loops.

When we use nested loops, then for 1 iteration of the outer loop, the inner Loop completes all iterations. In the 2nd iteration of the outer Loop, again, the inner Loop will complete its all iterations and so on.

Example

<?php for($a = 0; $a <= 2; $a++) { for($b = 0; $b <= 2; $b++) { echo "$b $a <br>"; } } ?>

Output

0 0
1 0
2 0
0 1
1 1
2 1
0 2
1 2
2 2

Explanation: In the example above, $a is incremented in the outer loop and $b in the inner loop. For $a=0 iteration, the inner loop will iterate thrice, $b = 0/1/2. Now same for 2nd outer loop $a=1, and third outer loop $a=2, the inner loop will execute thrice on each iteration.


PHP for-each Loop

The for-each loop in PHP specifically used to iterate through arrays. It is mostly used when we deal with database. The database return data in the form of associative arrays(key-value). To access the array, for-each loop iterate through the complete array and present the required data.

The for-each loop is easy to implement. It uses a temporary variable with the array identifier and iterates through it. There is no need to declare or initialize the loop counter variable.

Syntax
Syntax: foreach ($array as $value)
{
// code to be executed
}

Example

Output

Samsung
OnePlus
Xiaomi
Apple


PHP While Loop

The While loop is another way to execute tasks multiple times, just like for loop, but for loop is a bit more structured and rigid than while loop. You can choose any of these loops in most of the programs. It's your choice. But some specific scenarios are better handled by while loop.

When to use a while loop?

While loop is mainly used when we are not sure about the number of iterations of the loop. if we don't know about iterations, we won't initialize the counter variable in For Loop. In this case, we will use a while loop.

While loop doesn't necessarily depend on counters. So it can execute a loop in some situations where counter variables are not present. For example: Reading a file into a variable. A compiler reads files by characters, and we don't want the hassle to count the total characters, including spaces. So, While loops will efficiently iterate through each character until the end of the file, the loop will stop.

Syntax
while (condition is true)
{
// code to be executed
}

Example

<!DOCTYPE html> <html> <body> <?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?> </body> </html>

Alternative Syntax

You can also use the syntax in the way:

Syntax
while (condition is true)
// code to be executed
endwhile;

Example

<!DOCTYPE html> <html> <body> <?php $n=1; while($n<=5): echo "The number is: $n<br/>"; $n++; endwhile; ?> </body> </html>

Output

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

PHP Nested While Loop

Nested while loops are also possible, just like nested for loops. These also work like nested for loops. The inner loop will always do its all possible iterations for each outer loop's iteration.

Example

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

Output

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

PHP do-while Loop

The do-while loop is similar to the while loop but with a difference. In the While loop we first check the given condition and the loop executes only if it evaluates to true. . But in the do-while loop, the loop first executes one time and only then the given condition is checked. If the condition evaluates to true, only then the 2nd iteration happens in the do-while loop. Otherwise, the loop execution stops.

Syntax
do {
// code to be executed
endwhile;
}
while (condition is true);

Example

<!DOCTYPE html> <html> <body> <?php $x = 1; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?> </body> </html>

Output

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

Note : In do-while the loop executes at least once. The condition is evaluated/checked only after the completing one iteration of loop that's why the single iteration is guaranteed.











Follow Us: