PHP Looping

In this article, you will learn about the importance of loops and types of loops in Php. In programming, looping is used to run block of code again and again until some specific condition meet. Ultimately loops reduce human efforts, time and program length by eliminating the need of repetition of program statements.

There are four types of loops in Php:

WHILE loop

While loop executes the block of code as long as the condition is True. WHILE loop checks the condition first and if the condition is True, it will execute the block of code. The procedure repeats until the condition is False.

Syntax:

while(condition)
{
Block of code;
}

Let us take an example,

<?php 
$i = 1; 
while($i <= 3)
{
echo "The number is: 5*$i <br>";
$i++;
} 
?>

Output:

The number is:5

The number is:10

The number is:15

In this example the variable i is initialized to 1 in the beginning of the program. While loop checks the condition $i<=3, if it is True, it will execute the following lines of code and increments variable i. This process repeats until the condition is False. So when i is incremented to 4, while loop terminates.

DO…WHILE loop:

DO…WHILE loop will execute the block of code at least once. It will first execute the block of code and then it will check the condition. It repeats the procedure until the condition is False. As compared to WHILE loop, DO…WHILE loop has post condition check.

Syntax:

do
{
//Block of code;
} while(condition);

Let us take an example,

<?php 
$i = 1; 
do
{
echo "The number is: 5*$i <br>";
 $i++;
} while($i <=1) ;
?>

Output:

The number is:5

In this example the variable i is initialised to 1 in the beginning of the program. DO…WHILE loop executes the block of code once and then checks the condition. If the condition is True, then execution of the block of code will be repeated. The loop will terminate when the condition is False.

FOR loop

FOR loop executes the set of code as long as the condition is True. FOR loop checks the condition, if the condition is True then it will execute the block of code. The procedure repeats until the condition is False.

Syntax:

for (initialize counter; test condition; increment counter)
{
//Block of code;
}

Let us take an example,

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

Output:

The number is:1

The number is:2

The number is:3

In this example the variable i is initialized to 1 in the beginning of the program. FOR loop checks the condition and if it is True, then it will execute the block of code. The loop counter will be incremented and condition will be checked again. The loop will terminate when the condition is False.

FOREACH loop

FOREACH loop works only on array. FOR EACH loop executes the block of code for each key/value pair in an array.

Syntax:

foreach ($array as $value)
{
//Block of code;
}

Let us take an example,

<?php 
$numbers = array(1,2,3,4,5);
foreach ($numbers as $value)
{
echo "The number is: $value <br>";
}
?>

Output:

The number is:1

The number is:2

The number is:3

The number is:4

The number is:5

For every loop iteration, the value of array element is assigned to $value and array pointer will be incremented to point to the next array element. The process will continue till the last array element.

By | 2017-07-25T20:18:16+00:00 July 25th, 2017|php|0 Comments

About the Author:

Leave A Comment