PHP for loop: Explained with 6 examples
The for loop in PHP
PHP supports different types of loops to iterate through a given block of code.
These include for loop, while and do while and the foreach loop.
The for loop is the most complex loop that behaves like in the C language.
See also: the while loop
Structure of for loop
Following is the general structure to use the PHP for loop:
12345 for (expression1; expression2; expression3){statements to execute}
Where:
- The expression1 executes or evaluates first as iteration starts. This is generally used to initiate a variable.
For example: x = 1;
- The second expression normally is a condition that evaluates at each iteration. If it is true, the statements inside the curly braces will execute. If false, the control will move to the next line of code outside of for loop.
For example, x <=10;
- The expression3 executes at the end of each iteration that generally contains the increment or decrement operator. For example, x++.
See the following examples to learn more about for loop PHP.
An example of for loop to print numbers
In this example of for loop, I will display the numbers from 1 to 5. A little description of each step is given below.
PHP code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php //A demo of PHP for loop for( $x=1; $x<=5; $x++ ) { echo $x."<br>"; } ?> |
Output:
1
2
3
4
5
The first expression sets the initialization of variable $x i.e. $x = 1. The next expression sets the condition that keeps on running the for loop until the value of variable x is less than or equal to the value of 5.
The last expression increments the value of $x by 1 in each iteration. The statement in the curly braces will display the value of variable $x in each iteration.
An example of decrementing the last expression
In this example, the value of the last expression is decremented in each iteration.
The following for loop is used in the above example:
1 2 3 4 5 6 7 |
for( $x=5; $x>=1; $x-- ) { echo $x."<br>"; } |
Output:
The output of for loop:
5
4
3
2
1
It displayed the output in an HTML div element from 5 to 1.
An example of using increment operator as the last statement in for loop
You may also use expression3 inside the curly braces rather with the parenthesis after for keyword. The following example also displays numbers for 1 to 5 where the increment operator is used as the last statement inside the curly braces:
This is how the for loop is used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php //A demo of PHP for loop for( $x=1; $x<=5; ) { echo $x."<br>"; $x++; } ?> |
Output:
An example of incrementing by ten in for loop
In above examples, the value of variable x was incremented or decremented by 1 in each iteration. You may use some other number than 1. In this example, the value is incremented by 10 in each iteration. See the output and code below:
PHP code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php //A demo of PHP for loop for( $x=10; $x<=50; ) { echo $x."<br>"; $x= $x + 10; } ?> |
Output:
An example of using for loop with PHP array
It is common to use a for loop to iterate through arrays in PHP. In the following example, a numeric array is created with five elements. After that, a for loop is used with the array function (count) to get the total elements. We also mixed some HTML for display purpose:
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<!doctype html> <html> <head> <style> .fordiv { background: #9D9D00; height: auto; width:200px; border-radius: 15px; padding:20px; font-size:17px; color:#fff; } </style> </head> <body> <div class="fordiv"> The output of for loop:<br /> < ?php $arr_variable = array (10,20,30,40,50); for ($i=0; $i<count($arr_variable); $i++){ echo "The array item is: $arr_variable[$i] <br />"; } ?> </div> </body> </html> |
First of all, an array is created with five elements:
$arr_variable = array (10,20,30,40,50);
After that, the for loop is used to display the array elements:
1 2 3 4 5 |
for ($i=0; $i<count($arr_variable); $i++){ echo "The array item is: $arr_variable[$i] <br />"; } |
Output:
1 2 3 4 5 6 |
The output of for loop: The array item is: 10 The array item is: 20 The array item is: 30 The array item is: 40 The array item is: 50 |
The count function returns the total number of array elements that acted as specifying the highest number loop should go on.
A string array example with PHP for loop
In this example, a string array of three elements is created. After that, a for loop is used to display the array elements.
PHP code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $arr_course = array(); $arr_course[0] = "PHP"; $arr_course[1] = "MySQL"; $arr_course[2] = "Java"; for ($i=0; $i<count($arr_course); $i++){ echo "The element" . $i .":" .$arr_course[$i] ."<br />"; } ?> |
Output:
1 2 3 4 |
The output of for loop: The element [0]:PHP The element [1]:MySQL The element [2]:Java |