Register Now

Login

Lost Password

Enter your email to reset your password.

PHP loops is used to execute same block of code to a specified number of times. Loops are executed as long as the certain condition is true.

In PHP, we have following loop types:

  1. while – loops through a block of code as long as the given condition is true
  2. do…while – loops through a block of code atleast one, and repeats as long as the specified condition is true.
  3. for – loops through a specified number of times
  4. foreach – loops through a block of code for each element in an array

1. PHP While loop

The while loop loops through block of code as long as the specified condition is true.

Syntax

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

Example

 <?php
   $x = 1;

    while($x <= 10) {
     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
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10

2. PHP Do while loop

PHP Do while loop executes block of code atleast once, and then repeats as long as the specified condition is true.

Syntax

do {
    code to be executed;
} while (condition);
<?php
$x = 1;

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

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

In a while..do loop, loops executed first and the condition is checked at the end of loop. So, the loop will return result atleast once even if the condition is false.

Let us understand with the help of example –

<?php
$x = 10;

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

Output:

The number is: 10

3. PHP For loop

For loop is the important and most used loop everywhere. For loop is used to loops the block of code to a specified number of times.

Syntax

for(initialization;test condition;increment or decrement)
{
 block of statements;
}

OR

for(initialization;test condition;increment or decrement):
 block of statements;
endfor;

How PHP for loop work:

Let us understand with an example:

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

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

4. PHP Foreach loop

The PHP foreach loop loops through a block of code. It works only on arrays.

foreach loop is used to loop through each key/value pair in an array.

Syntax:

foreach($arrayName as $value)
{
 block of statement;
}

OR

foreach($arrayName as $value):
 block of statements;
endforeach;

OR

foreach($arrayName as $key=>$value)
{
 block of statement;
}

//$value is array's value
//$key is array's index or key

Let us understand with the help of example:

<?php
$subjects = array("Science", "English", "Math", "Computer");

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

Output:

Science
English
Math
Computer

The following example will output both the keys and the values of the given array ($subject_code):

<?php
$subjects = array("Science"=>"102", "English"=>"231", "Math"=>"121", "Computer"=>"200");

foreach($subjects as $x => $val) {
  echo "$x = $val<br>";
}
?>

Output:

Science = 102
English = 231
Math = 121
Computer = 200