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:

The for loop is the most complex loop that behaves like in the C language.

Structure of for loop

Following is the general structure to use the PHP for loop:

for (expression1; expression2; expression3){

statements to execute

}

Where:

Part Description
expression1 The expression1 executes or evaluates first as iteration starts.

This is generally used to initiate a variable.

For example: x = 1;

expression2 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;

expression3 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 a for loop, we will display the numbers from 1 to 5. A little description of each step is given below.

PHP code:

<?php

//A demo of PHP for loop

for( $x=1; $x<=5; $x++ )

{

echo $x."<br>";

}

?>

Output:

1
2
3
4
5

Explanation:

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

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 the increment operator as the last statement in for loop

You may also use expression3 inside the curly braces rather than 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:

<?php

//A demo of PHP for loop

for( $x=1; $x<=5; )

{

echo $x."<br>";

$x++;

}

?>

Output:

PHP for loop expression3

An example of incrementing by ten in for loop

In the 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:

<?php

//A demo of PHP for loop

for( $x=10; $x<=50; )

{

echo $x."<br>";

$x= $x + 10;

}

?>

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

The code:

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

for ($i=0; $i<count($arr_variable); $i++){

echo    "The array item is: $arr_variable[$i] <br />";

}

Output:

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:

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

The output of for loop:
The element [0]:PHP
The element [1]:MySQL
The element [2]:Java

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!