
For this week’s episode of Everything PHP, we are going to be talking about the final looping method in PHP. For loops are the most complex loop available in PHP. Being the most complex loop in PHP, they are also the most powerful. As with other loops in PHP, you can nest loops within other loops.
A for loop can accept multiple expressions to be evaluated. The basic for loop has three expressions, only one of which determines if the loop will be executed or not. The other expressions are for executing expressions whenever the loop is run (more on this in a bit). Here’s one of the most commonly used examples for the for loop:
[code='php']for ($i = 0; 10 > $i; $i++) {
echo $i;
}[/code]
Which results in the output of:
0123456789
The for loop is broken up into three groups of expressions, which are separated by semi-colons. The first group will only be run once, at the first iteration of the loop. The second group determines whether the loop will proceed or not. If the expression results to true, the loop will be executed, if the result is false, the loop will stop. The third group will be executed at the end of each loop iteration (after the code inside the loop has been executed).
The first expression group is used for setting variables that you will use within the second expression group. The second group is an expression which should result in either true or false (like something you would use in an if). You can use variables and functions (a note on using functions here later in the post) within this group. The third group is usually used for incrementing a variable used in the second group. Is it possible to leave any (or all) of the expression groups blank. If you leave the second expression blank, the loop will run indefinitely (which isn’t useless, as you may initially assume).
You can specify multiple expressions to be executed within any expression group. Simply separate the expression with commas. In the case of the second group, multiple expressions can still be used, but only the last expression in the list will be used to determine whether the loop will run or not.
When using functions in the second group, if you do not need the function to be run on each iteration of the loop, you should put the functions inside a variable in the first group. Otherwise, your loop will run slowly, because that function will be executed each time the loop iterates again. Take the example below, where we’re using count() on an array:
[code='php']$array = array('a', 'b', 'c');
for ($i = 0; $i <= count($array); $i++) {
echo $i;
}[/code]
Because the count of $array is not going to change, we do not need to calculate it with every iteration of the loop. Therefore, we should add in to a variable within the first group, which will only be run once.
[code='php']$array = array('a', 'b', 'c');
for ($i = 0, $array_count = count($array); $i <= $array_count; $i++) {
echo $i;
}[/code]
This will result in a faster loop.
Continue, Break and Infinite Loops
Remember how leaving the second group of expressions empty would result in an indefinite loop? Well, there actually are uses for this, because we can end the looping in a different way. We can use the break control structure to break out of the current loop (stop it from iterating again). Let's look at an example:
[code='php']for ($i = 0; ; $i++) {
if ($i == 26) {
break;
}
echo $i;
}[/code]
In this example, when $i reaches 26, the loop will be broken, and will stop iterating. If you have nested loops, you can specify the number of loops it should break out of (such as break 2;). break is different from exit in that the script will not end, just the current loop.
If is also possible to skip to the end of the current iteration of a loop, and continue on with the expression. We do this by using the continue control structure. It is used in the same way as break, and also accepts the number of loops it should skip to the end of.
break;
;) That concludes our article on for loops. Next week we'll be going over switch control structures.