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. See the example and code online which is followed by a little description of each step.
See online demo and code
The following code is executed in the example:
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>"; } ?> |
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.
See online demo and code
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>"; } |
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 increment operator is used as the last statement inside the curly braces:
See online demo and code
This is how the for loop was used in above example:
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++; } ?> |
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 online:
See online demo and code
The following PHP code 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=10; $x<=50; ) { echo $x."<br>"; $x= $x + 10; } ?> |
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 array function (count) to get the total elements and display in an HTML div tag.
See online demo and code
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 />"; } |
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.
See online demo and code
The following PHP code is executed in this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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 />"; } ?> |