Print this Section


For-Loop Structures

The for statement loop is used when you know how many times you want to execute a statement or a list of statements. For this reason, the for loop is known as a definite loop.

The basic for loop syntax is shown below:

  for (intialization; condition; increment) {
    code to be executed;

The for statement includes three parameters. The first parameter is used to initialize variables, the second holds the condition, and the third parameter contains any increments required to implement the loop. The code block associated with the for statement is always enclosed within the { opening and } closing brace symbols.

The following example demonstrates a for loop that displays the message "Welcome to PHP" 4 times:

  for ($counter=1; $counter < 5; $counter++) {
     echo "Welcome to PHP <br>";


In the above example, the for loop includes three parameters that perform the following tasks:

  1. The variable $counter is initialized to 1.
  2. $counter < 5 establishes the condition that must be true for the loop to execute.
  3. $counter++ increments the variable $counter each time the loop runs

Below is the output generated by the sample loop:

   Welcome to PHP 
   Welcome to PHP 
   Welcome to PHP 
   Welcome to PHP 

During the first run, the value of $counter is initialized to 1. Since 1 is less than 5, $counter is incremented to 2. Next, the echo statement is used to display the string "Welcome to PHP". A <br> is concatenated to the display to create a carriage return each time the loop is processed. During the second iteration, the value of $counter is equal to 2. Since 2 is less than 5, the echo statement again displays the string "Welcome to PHP". This process continues while the value of $counter is 3 and 4. When $counter is equal to 5, the condition is no longer true and the while loop ends.

for loops are also used as a convenient way to iterate through the values of an array. Recall from 3.2 Array Variables section that arrays consists of elements and indexes. Each element has an associated index. The first index of a numerically indexed array is 0. Until now, when printing array elements, it was necessary to print each element separately. For large arrays, this can be very time consuming and cumbersome. for loops solve this problem. In the following example, an array containing 5 elements is created. Next, a for loop is used to display each of the array values.

  //Create a new array containing 5 color values
  $colors = array('red', 'green', 'blue', 'yellow','white');

  //Use a for loop to iterate through the array and display each element

  for ($i = 0; $i < sizeof($colors); $i++) {
    echo "Array value $i+1 is $colors[$i].";	


The $colors array contains five elements - $colors[0] = "red", $colors[1] = "green", $colors[2] = "blue", $colors[3] = "yellow", and $colors[4] = "white". A for loop is created; the counter variable $i is initially set to 0 to correspond with the first element of the array. Next, the condition is set by checked to see if the value of the counter, $i, is less than sizeof($colors) or the total number of elements in the array. Here we know the size of the array is 5, however, in most cases the size of the array at runtime is unknown. Finally, the counter variable is incremented by 1 during each iteration. Each time the loop runs, the string - "Array value $i+1 is $colors[$i] is displayed. The values of $i+1 and $colors[$i] are expanded and produce literal values when printed. The loop runs until the the counter exceeds the number of elements or the size of the array. The output is show below:

   Array value 1 is red. 
   Array value 2 is green.
   Array value 3 is blue. 
   Array value 4 is yellow.
   Array value 5 is white.

Loops are useful constructs that will be used extensively throughout future tutorials.

TOP | NEXT: foreach loops