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. See the example and code online which is followed by a little description of each step.

PHP for

See online demo and code

The following code is executed in the example:

 

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.

PHP for loop

See online demo and code

The following for loop is used in the above example:

 

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:

PHP for loop expression3

See online demo and code

This is how the for loop was used in above example:

 

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:

PHP  for loop increment 10

See online demo and code

The following PHP code is used:

 

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.

PHP for loop array

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:

 

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 for loop array string

See online demo and code

The following PHP code is executed in this example: