[PHP Tutorial 3] For and Foreach Loops in PHP

0

We discussed the conditional statements, using if else, else if and switch in tutorial 2 but there are more ways to use conditional statements. Actually, it becomes cumbersome to write the same statement again and again if the conditional statements like if else have been used a number of times. To avoid repeating the statements, we use loops. You just need to mention the condition in the loop and the statement will get executed the number of times it has been defined so.

Let’s see what kind of loops we can use. We have, for loop, foreach loop, while loop, do-while loop:

 For loop

 The basic syntax for using for loop is

 

For (initialization; condition; increment/decrement)

{

            Your statement;

}

 

Let’s take an example of php program using for loop:

 

<?php

 for($i=0; $i<5; ++$i)

 {

echo " the value of i is $i <br>";

}

 ?>

Here, “i” variable is defined and designated the value “0”. This is the first expression in the for loop and is termed as initialization. The second expression defines the condition, that value of “i” should be less than 5. The third expression is an increment statement by which the value of “i” will be increased by 1. It can written either ++$i, which is called pre-increment operator or by $i++ which is called post-increment operator.

++$i is nothing but the statement, $i=$i+1. For example, if the value of “i” was 3; 1 will get added to it and the new value, i.e, 4 is assigned to the variable “i”. While the post-increment operator, assigns the value first and then add 1 but the pre-increment operator, adds 1 before assigning the value. Decrement operators are also of two types, eg. – – $i and $i- –

Now, coming back to the loop; the value of i is displayed when the conditions in the for loop is true. The output of the above program is:

the value of i is 0
the value of i is 1
the value of i is 2
the value of i is 3
the value of i is 4

We will see how this happened. At first, the first expression of the for loop says the value of i is 0; then according to the next expression, 0 is less than 5 which is also true. The third expression increments its value to 1, so now the flow goes to the next statement, ie. “the value of i is 0” gets displayed. Now, value of i; 1, is passed again to the for loop to check if it is less than 5, then increments to 2 and “the value of i is 1” gets displayed. When the value of i becomes 5, the second expression becomes false and the loop exits.

 <br> is used to give breaks within the lines and the output is shown in the next line. If we don’t use it, all the outputs will be shown in one line (consecutively).

Foreach loop

Before going for foreach, we should be aware of arrays because foreach is used with arrays.

An array is a collection of variables of same data type. If we have more than one variable and do not want to define them separately, we can use array. Eg: $a=array(1,2,3,4,5,6); or we can also write it as follows:

 $a[0]=1;

$a[1]=2;

$a[2]=3;

$a[3]=4;

$a[4]=5;

$a[5]=6;

 

Let us see how to use foreach with arrays:

The syntax for using foreach is:

 foreach(array_name as array_value)

{

            Your statement;

}

 

Where array_name is the name of the array variable and array_value is the variable used for the values taken from the array.

Example:

 <?php

$arr=array(10,20,30,40,50);

foreach ($arr as $i)

 {

echo " the value of array is $i <br>";

}

?>

 

The output of the above program will be:

 

the value of array is 10
the value of array is 20
the value of array is 30
the value of array is 40
the value of array is 50

In this loop, for every value in the array, statement written after echo is displayed. Loop will first take 10 and displays the echo statement, then 20 and so on. Each value in the array will be recognized by the loop as $i variable.

Now that you know how to use FOR and FOREACH loop in PHP, continue with the next PHP tutorial on how to use DO and DO WHILE loop in php. Also, if you have any queries, please leave a comment below.

Share.

Leave A Reply