Hit enter after type your search item

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:


 

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:


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:


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:


Output:

PHP for loop expression3

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:


Output:

PHP for loop increment 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 the array function (count) to get the total elements. We also mixed some HTML for display purpose:

The 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:


Output:

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:


Output:

 
This div height required for enabling the sticky sidebar