Udjuni.com

udjuni.com search:




Looping in PHP


In the posting “PHP flow control functions”, we covered how one can implement decision making in a PHP program, in which we covered validating predefine conditions and executing a certain portion of the program or script. Here, we are going to look at automating repetitive work.

Say for example, If we needed to add a number 2 to every integer between 1 and 10 and display the answers, this would require us to hard-code the expression and answer for each integer. In other words, this is what our program will look like:
echo 0 + 2 =  2;
echo 1 + 2 =  3;
echo 2 + 2 =  4;
…

As you can already tell, this is a very rigid way of doing it, a long method and programs written this way are not easy to maintain. The good news is that PHP and most other programming languages have a way of automating such repetitive functions, by looping on a set of values for a certain number of times or until a certain specified condition is met. Among the methods to loop through a set of values:

The while Statement


The general format for a while statement is as follows:
while(expression){
    //do whatever you want in here.
}
This will keep repeating the execution of the code in the parenthesis as long as the expression evaluates to TRUE. For such a statement to be executed, the expression is first evaluated, if it evaluates to TRUE the block of statements within the parenthesis is executed, if the result is false the code in parenthesis is skipped. To keep the program from looping forever, you would have to indicate the starting point.
For example,
 $counter = 0;
   while($counter <= 10){
                echo “$counter + 2 = “. ($counter + 2);
                $counter++; //increment the value for counter by 1
           }

This will loop through the code displaying the sequence of sums of 2 and numbers between 1 and 10.

The do …while Statement



This kind of statement is executed at least once, at the end of the first iteration or loop the expression is evaluated, if it evaluates to true, the statements within the parenthesis following the do is executed. This can go on for as long as it needs to be. The general expression for a do…while statement is:
do {
	//do whatever that needs to be done here
       }while (expression);                                   
With a real life example as in the previous one:
$counter = 0;
    do {
          echo “$counter + 2 =”.($counter + 2);
          $counter ++;
       } while ($counter <= 10);

Note that because $counter is initially assigned a starting point, because that value is less than 10, the statements in the parenthesis following the do will be executed repeatedly while incrementing the value for $counter by one with each iteration. Now, if we assigned the number 45 instead of 0 at the beginning the code in this whole block would never be executed more than once. Why? Because 45 would never be less than or equal to 10, thus the expression in the while would always be false.

The for Statement


This statement is used when you want to execute the code for a predefined number of times. The general format for the for statement is:
for(initialization_expression; test_expression; modification_expression){
	   //do whatever you want here;
	}
Using a practical example as in the previous cases:
 for( $counter = 0; $counter <= 10; $counter++){
                 echo “$counter + 2 = ”.($counter + 2);
           }




RELATED ARTICLES:


Oracle Procedures, Functions and looping constructs
Procedures and functions in PL/SQL are types of blocks that are stored in the database in a compiled form. This is among the major differences between ordinary queries and procedures and functions (otherwise known as subprograms). Creating a function or procedure requires the u

What the cash flow statement tell us
The cash flow statement is a financial document that shows how changes in balance sheet and income statements affect cash and cash equivalents. As an analytical tool, the cash flow statement is useful in determining the short-te

The Anatomy of an IF statement
In programming, there are cases when you need the program to perform a certain function when a certain condition is met. If such conditions are met then a certain piece of code is executed, otherwise skip it and execute an alternative code. This is ac

Breaking out of loops with break statement
div class='floatleft'> So far, we have discussed how the while and for loops work. Assuming that you were looping through a set of values, dividing each value by a number between -10 and 10. Given that you are aware of the rule that "Anything divided by zero

while loop


for loop


Did not find it? Try udjuni.com search:







© 2009 UdjunI LLC. All rights reserved | Privacy policy | Tutorial Index | RSS Feed