Site icon Bootsity

Looping around in PHP

1. Introduction

In this article in the series, we are learning about different types of loops. Loops are programming constructs that can be used to repeat a particular task, or in other words, we can say loops are used to execute the same block of code again and again by some pre-defined conditions. The primary use of loop is to automate repetitive tasks and save time and efforts.
In PHP, we have the following types of loops:
Let us go through them to understand each loop deeply.

2. PHP while loop

The while loop executes the code block until the condition results in true. The syntax of while loop is:
while(condition){
    // Code to be executed
}
The code below demonstrates the use of while loop which starts $i = 1  with  and keeps executing until the condition $i <= 5 results in true.
$i = 1;
while($i <= 5){
    $i++;
    echo "Value of $i is " . $i . "<br>";
}
Output:
Value of $i is 1
Value of $i is 2
Value of $i is 3
Value of $i is 4
Value of $i is 5

3. PHP do-while loop

The do-while loop is a modified version of while loop. The while loop evaluates the condition at the starting of iteration, but the do while evaluates the condition at the end of every iteration, after execting it once.
The do-while loop always executes the loop once and keeps iterating until the condition results in true. The syntax of do-while is:
do{
    // Code to be executed
}while(condition);

The code below demonstrates the use of do-while loop which starts with $i = 1 and keeps executing until the condition $i <= 5  results in true.

$i = 1;
do{
    $i++;
    echo "Value of $i is " . $i . "<br>";
}while($i <= 5);

Output:

Value of $i is 1
Value of $i is 2
Value of $i is 3
Value of $i is 4
Value of $i is 5

4. PHP for loop

The for loop is the most frequently used loop. It repeats a block of code until a certain condition is true. The syntax of for loop is:
for(initialization; condition; increment){
    // Code to be executed
}

Let’s discuss the three parameters of for loop:

Consider:
for($i=1; $i<=5; $i++){
    echo "Value of $i is " . $i . "<br>";
}
The output of the above code would be:
Value of $i is 1
Value of $i is 2
Value of $i is 3
Value of $i is 4
Value of $i is 5

5. PHP foreach loop

The foreach loop is mostly used to iterate over arrays. The syntax is:
foreach($array as $value){
    // Code to be executed
}
The following example demonstrates the use of foreach loop that will print the values of the given array:
$planets = array("Earth", "Mars", "Venus");

// Loop through $plants array
foreach($planet as $planet){
    echo $planet . "<br>";
}
The output would be:
Earth
Mars
Venus
There is one more syntax of the foreach loop, which is an extension of the above syntax.
foreach($array as $key => $value){
    // Code to be executed
}
The $key contains the key of the array. So, the above example becomes –
$planets = array("Earth", "Mars", "Venus");

// Loop through $plants array
foreach($planet as $key => $planet){
    echo $planet . " is at position ". $key ."<br>";
}
The output would be:
Earth is at position 0
Mars is at position 1
Venus is at position 2

6. Conclusion

In this article, we had an in-depth look at different types of looping constructs and how and where we can use them. In the next article of this series, we will learn about different HTTP related capabilities of PHP programming language. Till then, try printing table of 2 using for loop.
Exit mobile version