Loops in program are a branch of codes which are repeatedly executed again and again until the condition attached to the loop has been satisfied. Say, we have a block of code which can calculate the aggregate of marks obtain by a student and we have total ten students in a class room. In this case we have to use loop to repeat the code block ten times to calculate the aggregate of all ten student. So loops are very essential and useful tool in coding a program.
PHP language offers many types of loop statements. They are,
A brief introduction with example of each of them is given below.
for loops are the most complex loops in PHP. The syntax of a for loop is:
for(expr1; expr2; expr3){code to be repeated}
In this syntax the condition of the loop consists of three expressions, - exep1, exep2, exep3.
Functioning of "for loop" can be very clearly explained with the following example.
for($i=0; $i<10; $i++){Codes to be repeated}
Here the first expression $i=0, set the initial value of variable $i to zero before the loop start. The third expression $i++, increase $i by one in each successive repetition of the loop code. The second expression $<10 evaluated before each repetition start and if this expression is satisfied only then the loop code will be repeated. Clearly in this example the loop code will be repeated for ten times ($i=0 to $i=9) as long as $i less than 10.
The foreach loop statement in PHP is used run the loop code over each variable of an array. Here is an example of "foreach" statement.
<?php
$arr = array('PHP', 'loop', 'for', 'foreach');
foreach ($arr as $value){echo $value.'<br />';}
/* Output of this code on browser window is
PHP
loop
for
foreach
*/
?>
In this example each element in the array $elements will be printed on browser window. If we try to realize the same example with "for" loop the resulting code will be little more complex.
Here is another example of "foreach" loop for an associative array.
<?php
$pagevars=array('pagename'=>'php-loops','title'=>'PHP loops','keywords'=>'loop,PHP loops,while,goto,foreach,do-while,Infinite loop','description'=>'PHP language offers many types of loop statements. They are for loop, foreach loop, while loop, do-while loop, goto loop. A brief introduction with example of each of them is given here');
foreach($pagevars as $key=>$value){echo '<b>'.$key.'</b>: '.$value.'<br />';}
/* Output of this code on browser window is
pagename: php-loops
title: PHP loops
keywords: loop,PHP loops,while,goto,foreach,do-while,Infinite loop
description: PHP language offers many types of loop statements. They are for loop, foreach loop, while loop, do-while loop, goto loop. A brief introduction with example of each of them is given here
*/
?>
The syntax of "while" loop is of the following form.
while(expression){code to be repeated}
At the beginning of each repetition of loop code the expression is evaluated and if it is satisfied only then the loop code will be repeated. An example of PHP loop using "while" statement is given below.
<?php
$i=5;//set initial value
while($i){
echo $i.'<br />';
$i--;//Decrease $i by 1 in each repetition of the loop.
}
/* Output of this code on browser window is
5
4
3
2
1
*/
?>
The "do-while" loop statement is very much similar to "while" loop except here code is first executed before verifying the loop condition. At the end of each repetition loop condition is evaluated and if satisfied then the loop code will be repeated again. The following example will show the difference between "while" and "do-while" loop.
<?php
$i=5;//set initial value
do{
echo $i.'<br />';
$i--;//Decrease $i by 1 in each repetition of the loop.
}while($i)
/* Output of this code on browser window is
5
4
3
2
1
*/
?>
Actually "goto" operator is an unconditional jump statement used in PHP from version 5.3. The syntax of "goto" statement is,
goto lebel
By using "goto" operator and "if" statement you can create a successful loop in PHP. Here is an example of such loop with goto operator.
<?php
$i=5;//set initial value
loop:
echo $i.'<br />';
$i--;
if($i){goto loop;}
//Next statements.
?>
An infinite loop or endless loop is a loop program where loop code is endlessly repeated. Such situation may arise because of error in coding when loop condition is endlessly satisfied causing an infinite number of repetition of loop code. Here are few examples of such loops.
So while writing loop program programmers must be very careful that in all possible condition loops must be ended within a limit.