PHP – Loops | for-loop, while-loop, do-while loop

Today we will learn about PHP – Loops. These are very important loops that are necessary before writing a PHP program. Before these, we also discussed PHP Control Structures: If else, Switch Case, Data Type in PHP:, Operators In PHP, if you don’t know then Hurry up Now! 

PHP – Loops

A Loop is known as the Iterative Control Structure in which until a particular condition is met it executes the same number of codes simultaneously.

PHP supports four types of loops.

  1. for loop
  2. while loop
  3. do-while loop
  4. foreach loop

PHP – for Loop

This loop is used when the user knows early, how many times the block needs to execute. These loops are also known as entry-controlled loops. Here mainly three parameters are used in the code, namely the initialization, the test condition, and the counter.

Syntax:  <?php
	for (initialize; condition; increment){
	//code to be executed
        }
        ?>

for Loop – Flow Chart :

Example:     In the below example makes five iterations:

<!DOCTYPE html>
<html>
<body>
<?php  
for ($x = 0; $x <= 5; $x++) {
  echo "The number is: $x <br>";
}
?>  
</body>
</html>
 OUTPUT:
 The number is: 0
 The number is: 1
 The number is: 2
 The number is: 3
 The number is: 4
 The number is: 5

Example Explained

  • $x = 0; – Initialize the loop counter ($x), and set the start value to 0
  • $x <= 5; – Continue the loop as long as $x is less than or equal to 10
  • $x++ – Increase the loop counter value by 1

PHP – While Loop

           They are used to execute a block of statements repeatedly until the set condition gets satisfied.

When the test expression is true then the statement block will be executed. After the statement has been executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.

 Syntax:
 <?php
 while (condition){
 block of code to be executed;
 }
 ?>

Example:

<!DOCTYPE html>
<html>
<body>

<?php  
$x = 1;
 
while($x <= 10) {
  echo "The number is: $x <br>";
  $x++;
} 
?>  

</body>
</html>
 OUTPUT: 
 The number is: 1
 
 The number is: 2
 The number is: 3
 The number is: 4
 The number is: 5
 The number is: 6
 The number is: 7
 The number is: 8
 The number is: 9
 The number is: 10

Example Explained

  • $x = 1; – Initialize the loop counter ($x), and set the start value to 1
  • $x <= 10 – Continue the loop as long as $x is less than or equal to 10
  • $x++; – Increase the loop counter value by 1.

While Loop–Flow Chart :

PHP – do while Loop

This loop will always execute the block of a statement once, it will then check the condition, and repeat the loop while the specified condition is true.

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

do while Loop–Flow Chart :

Example:

<!DOCTYPE html>
<html>
<body>

<?php 
$a = 1;

do {
  echo "The number is: $a <br>";
  $a++;
} while ($a <= 10);
?>

</body>
</html>
 OUTPUT: 
 The number is: 1
 The number is: 2
 The number is: 3
 The number is: 4
 The number is: 5
 The number is: 6
 The number is: 7
 The number is: 8
 The number is: 9
 The number is: 10

Remember that, In a do…while loop, AFTER executing the statements within the loop the condition has been tested. This means that the do…while loop will execute its statements at least once, even if the condition is false.

 PHP – foreach Loop

This loop works only on arrays and is used to loop through each value pair in an array.

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

In every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one until it reaches the last array element.

foreach-Loop flowchart:

Example:

<!DOCTYPE html>
<html>
<body>

<?php  
$bikes = array("Hero", "Honda", "TVS", "BMW"); 

foreach ($bikes as $value) {
  echo "$value <br>";
}
?>  

</body>
</html>
 OUTPUT:
 Hero
 Honda
 TVS
 BMW

You Might Like

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry