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);
}
|