PHP for loop | Types of Loop

Loops in PHP are used to execute the block of code specified number of times. If you have a need to print tables 1 to 20 then you have specified loop initiate from 1 to 20. This condition will be specified in loops. Loops are the methods of PHP, which used to set the compiler work limit of the number. The compiler checks the condition and executes the code if the limit does not end. If the limit is ended, the compiler gets exit from the code of block (Exit from the loop)

Types of Loop

for loop

Loop through the block of code specified number of time execute.

While loop


Loops depend on the condition if and as long as a specified condition is true.

do-while loop


First, execute the code ,and then repeats the loop as long as a specified condition is true.(first check the statements then go to the condition ).

foreach loop -


To perform an action for each value in an Array .Use values different-different way in time .

For loop in PHP :-

In for loop you have to specify initial value and condition. Block of code will execute until the condition is true when the condition is false compiler will be the exit of the block .


for (initialization; condition; increment)
code to be executed;


- Where the value begins.(ex. $i=0)


- Specify condition here . (ex. $i<20)


- Value of $i will be increment until the condition is true.

Examples of For Loop

In the following example create a table using for loop .

Print a table

for( $i=0; $i<20; $i++ )
  echo "
"; echo $i; } ?>
$i=0; --  Initial Value of $i(variable) is zero .
$i<20; --   Value of $i lessthan 20 .
$i++  --   Value will be increment .

Array with For Loop

In the earlier tutorials, we have discussed Arrays . Now we will add A -Z letters inside the array with the help of for Loop. If you add A -Z letters in the array by yourself, then it takes a lot of time and there is a lot of complexity in adding letters inside the array. But with the help of For Loop, you can easily store A TO Z Letters. We use the array_push() function to add elements on to the end array. Let's create an empty array using array() function and add elements through the for loop and array_push function.


$letters = array();
for ($i= 'A'; $i != 'AA'; $i++)
    array_push($letters, $i);
echo print_r($letters, true);


    [0] => A
    [1] => B
    [2] => C
    [3] => D
    [4] => E
    [5] => F
    [6] => G
    [7] => H
    [8] => I
    [9] => J
    [10] => K
    [11] => L
    [12] => M
    [13] => N
    [14] => O
    [15] => P
    [16] => Q
    [17] => R
    [18] => S
    [19] => T
    [20] => U
    [21] => V
    [22] => W
    [23] => X
    [24] => Y
    [25] => Z

In this above example, we created an array. After that, we made the array variable. Inside the for loop, we created a different variable and initialized from A.After that a condition is created and the variables are incremented. These are the most important fact of for loop that you create a condition for compiler works limit. In the array_push () function we have passed 2 parameters. The first parameter has an array variable and the second parameter has another for loop variable. We used print_r() function to display the array standard formate.

Please Share

Recommended Posts:-