Looping in PHP

Looping in PHP
Many a times we want to do some specific calculations repeatedly. To do so PHP provides the following looping statements.

We will learn about these iteration methods one by one.


The syntax of a while statements in PHP as given below.


while(condition)
{
do this;
}

  • The PHP continues to "do this" till the "condition" remains false.
  • The condition is evaluated at the beginning of each iteration.
  • If the condition is false, at the beginning of any iteration (even the first one), the PHP comes out of the loop.

The syntax of a while statement given above is the most commonly used syntax. Alterantively if the chunk of PHP statements to be executed is large, you can use the endwhile instead of the pair of braces. The syntax of doing so is as below.


while(condition)
do this;
and this;
endwhile

Example: The following code will print the the numbers from 1 to 10 one after the other. Use this if only one statements has to be iterated over.

$i = 0;
while($i<10)
echo $i++."<br>";

Example: The following code will again print the numbers from 1 to 10 two in a line; Use this (Or the next method) method if multiple statements are to be iterated over.


$i = 0;
while($i<10)
{
echo "Cureent number is";
echo $i++."<br>";
}

Example: The following code will give the same output as the above example. Notice the usage of colon ":" and semicolon ";". Use this (Or the next method) method if multiple multiple statements are to be iterated over.


$i = 0;
while($i<10):
echo "Cureent number is";
echo $i++."<br>";
endwhile;


The syntax of a do-while statement is as given below.

do{
this;
}while(condition)
  • Unlike the while statements, do while condition is checked at the end of each iteration, guranteeing that the loop will be executed at least once. Use this loop when you have any such requirement.
  • do-while loop in PHP is executed until the condition becomes FALSE.
  • You can use do while loops also in place of while statements (though not recommended) by using proper break statements (A break statement in PHP loop takes you out of the immediate loop irrespecitve of the looping condition).

Example: The following code prints the values 1 to 10 one after another;

$i=0;
do{
echo $i++."<br>";
}while($i<=10)

Notice the difference from the similar loop using the while statement.


The syntax of a PHP for loop is as given below.

for(statement1; condition; statement 2)
{
do this; //<- chunk of statement to be
// iterated over.
}

//statement1 <-Executed once only at the beginning of the loop
//condition <-Evaluated before starting each iteration (The first
// one also)
//statement 2 <-Executed at the end of each iteration.


The for loop is one of the most used loops in PHP. This loop is very much similar to the for loop in C and Java.

Most of the statement 1 is the initialization of a counter variable, condition is the condition which is to be evaluated and statement 2 is the increment or decrement statement.

  • None of the three expressions are mandatory, However you must use the semicolon.
  • Just like PHP while, PHP for also has its endfor. Syntax is similar to endwhile.

Example: This example outputs the numbers from 1 to 10. Use this if you have to execute only one PHP statement.

for($i=1;$i<=10;$i++)
echo $i."<br>";

Example: This example again outputs the numbers from 1 to 10. Include your PHP statements inside a pair of braces if you want to execute multiple PHP statements at a time.


$i=0;
for(;$i<10;)
{
echo $i++."<br>";
}

Example: This example again outputs the numbers from 1 to 10. Notice the usage of colon(:) and semicolon(;).


for($i=1;$i<=10;):
echo $i."<br>";
$i++;
endfor;


The foreach statement is a special looping facility to iterate over array elements. The syntax of a foreach statement is as given below.


foreach (array as value)
statement;

This will become more clear by examples. Example: Suppose that we have an array of five integers. We want to multiply each of these elements by 4. We can do so by using a while loop as below.


$array = array(1,2,3,4,5);
$i=0;
while(i<5){
$array($i) = $array($i) * 4;
}

We can do the same thing by using the foreach loop as below.


$array = array(1,2,3,4,5);
foreach($array as $value)
$value = $value * 4;

Notice that here $value acts as the array element at any iteration. Not the value only. Because the changes to this $value are being stored as the changes in the array elements.

Due to this we need to unset the reference to the last array element after the loop if we do not wish to let the reference remain. We do so by using the unset() function. This function breaks the refernce of the variable $value with the array $array element. Note that if you come out of the loop using some break statement, then also the $value will have the reference to the last accessed array element. unset($value) destroys all such references.

We can also access each of the array elements of an associative array with their respective key ids as below.


$array = array("name","age"=>"john","23");
foreach($array as $key => $value)
{
echo $key;
echo $value;
}