PHP While, Do-While, For and Foreach Loops

PHP While, Do-While, For and Foreach Loops

PHP While, Do-While, For and Foreach Loops | loops in php 2022

php types of Loops – In this tutorial you will learn how to use PHP while, do-while, for and foreach loops to automate the repetitive tasks within a program to save the time and effort.

 

Different Types of Loops in PHP

Loops are used to execute the same block of code again and again, as long as a certain condition is met. The basic idea behind a loop is to automate the repetitive tasks within a program to save the time and effort. PHP supports four different types of loops.

  1. while — loops through a block of code as long as the condition specified evaluates to true.
  2. do…while — the block of code executed once and then condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true.
  3. for — loops through a block of code until the counter reaches a specified number.
  4. foreach — loops through a block of code for each element in an array.

You will also learn how to loop through the values of array using foreach() loop at the end of this chapter. The foreach() loop work specifically with arrays.

You may like

  1. PHP while loop: 

The while loop is also an entry control loop like for loops i.e., it first checks the condition at the start of the loop and if its true then it enters the loop and executes the block of statements, and goes on executing it as long as the condition holds true.

The while loop executes a block of code as long as the specified condition is true.

Syntax

while (if the condition is true) {

    // code is executed

}

Example:

// PHP code to illustrate while loops

$num = 2;

while ($num < 12) {

    $num += 2;

    echo $num, "n";

}

Output:

 4

6

8

10

12

Example 2.

$x = 1;

while($x <= 5) {

  echo "The number is: $x <br>";

  $x++;

}

Output:

The number is: 1

The number is: 2

The number is: 3

The number is: 4

The number is: 5

 

Example Explained

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

PHP do while Loop :-

The do-while loop is a variant of while loop, which evaluates the condition at the end of each loop iteration. With a do-while loop the block of code executed once, and then the condition is evaluated, if the condition is true, the statement is repeated as long as the specified condition evaluated to is true.

Syntax

do{

    // Code to be executed

}

while(condition);

The following example define a loop that starts with $i=1. It will then increase $i with 1, and print the output. Then the condition is evaluated, and the loop will continue to run as long as $i is less than, or equal to 3.

 

Example 

$i = 1;do{ $i++; echo "The number is " . $i . "<br>";}while($i <= 3);

Output:

The number is 2

The number is 3

The number is 4

 

The PHP for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax

for (init counter; test counter; increment counter) {

  code to be executed for each iteration;

}

Parameters:

  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment counter: Increases the loop counter value

 

The parameters of for loop have following meanings:

  • initialization — it is used to initialize the counter variables, and evaluated once unconditionally before the first execution of the body of the loop.
  • condition — in the beginning of each iteration, condition is evaluated. If it evaluates to true, the loop continues and the nested statements are executed. If it evaluates to false, the execution of the loop ends.
  • increment — it updates the loop counter with a new value. It is evaluate at the end of each iteration.

The example below defines a loop that starts with $i=1. The loop will continued until $i is less than, or equal to 3. The variable $i will increase by 1 each time the loop runs:

Examples :-

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

Examples 2.

// code to illustrate for loop

for ($num = 1; $num <= 10; $num += 2) {

    echo "$num n";

Output:

1

3

5

7

9

The PHP foreach Loop

The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

Syntax

foreach($array as $value){

    // Code to be executed

}

Example:

 $arr = array (10, 20, 30, 40, 50, 60);

    foreach ($arr as $val) { 

        echo "$val n";

    }

    $arr = array ("Ram", "Laxman", "Sita");

    foreach ($arr as $val) { 

        echo "$val n";

    }

Output:

 10

20

30

40

50

60

 

ram

laxman

site

Example:

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {

  echo "$x = $val<br>";

}

Output:

Peter = 35

Ben = 37

Joe = 43

 

Difference Between while and do…while Loop

The while loop differs from the do-while loop in one important way — with a while loop, the condition to be evaluated is tested at the beginning of each loop iteration, so if the conditional expression evaluates to false, the loop will never be executed.

With a do-while loop, on the other hand, the loop will always be executed once, even if the conditional expression is false, because the condition is evaluated at the end of the loop iteration rather than the beginning.

You may like PHP While, Do-While, For and Foreach Loops

Leave a Comment

Your email address will not be published. Required fields are marked *