PHP has four ways you can execute a block of code multiple times: while, for, foreach, and do...while. Of the four, only do...while sees little use; the others are popular and you will certainly encounter them in other people's scripts.

The most basic loop is the while loop, which executes a block of code for as long as a given condition is true. So, we can write an infinite loopa block of code that continues foreverwith this PHP:

The loop block checks whether $i is greater or equal to 10 and, if that condition is true, adds 1 to $i and prints it. Then it goes back to the loop condition again. Because $i starts at 10 and we only ever add numbers to it, that loop continues forever. With two small changes, we can make the loop count down from 10 to 0:

So, this time we check whether $i is greater than or equal to 0 and subtract 1 from it with each loop iteration. while loops are typically used when you are unsure of how many times the code needs to loop because while keeps looping until an external factor stops it.

With a for loop, you specify precise limits on its operation by giving it a declaration, a condition, and an action. That is, you specify one or more variables that should be set when the loop first runs (the declaration), you set the circumstances that will cause the loop to terminate (the condition), and you tell PHP what it should change with each loop iteration (the action). That last part is what really sets a for loop apart from a while loop: You usually tell PHP to change the condition variable with each iteration.

We can rewrite the script that counts down from 10 to 0 using a for loop:

<?php for($i = 10; $i >= 0; $i -= 1) { echo $i;

This time we do not need to specify the initial value for $i outside the loop, and neither do we need to change $i inside the loopit is all part of the for statement. The actual amount of code is really the same, but for this purpose the for loop is arguably tidier and therefore easier to read. With the while loop, the $i variable was declared outside the loop and so was not explicitly attached to the loop.

The third loop type is foreach, which is specifically for arrays and objects, although it is rarely used for anything other than arrays. A foreach loop iterates through each element in an array (or each variable in an object), optionally providing both the key name and the value.

In its simplest form, a foreach loop looks like this:

This loops through the $myarr array we created earlier, placing each value in the $vaiue variable. We can modify that so we get the keys as well as the values from the array, like this:

<?php foreach($myarr as $key => $value) { echo "$key is set to $value\n";

As you can guess, this time the array keys go in $key and the array values go in $vaiue. One important characteristic of the foreach loop is that it goes from the start of the array to the end and then stopsand by start we mean the first item to be added rather than the lowest index number. This script shows this behavior:

$array = array(6 => "Hello", 4 => "World",

2 => "Wom", 0 => "Bat"); foreach($array as $key => $value) { echo "$key is set to $value\n";

If you try this script, you will see that foreach prints the array in the original order of 6, 4, 2, 0 rather than the numeric order of 0, 2, 4, 6.

The do. . .while loop works like the while loop, with the exception that the condition appears at the end of the code block. This small syntactical difference means a lot, though, because a do. . .while loop is always executed at least once. Consider this script:

Without running the script, what do you think it will do? One possibility is that it will do nothing; $i is set to 10, and the condition states that the code must loop only while $i is less than 10. However, a do...while loop always executes once, so what happens is that $i is set to 10 and PHP enters the loop, decrements $i, prints it, and then checks the condition for the first time. At this point, $i is indeed less than 10, so the code loops, $i is decremented again, the condition is rechecked, $i is decremented again, and so on. This is in fact an infinite loop, and so should be avoided!

If you ever want to exit a loop before it has finished, you can use the same break statement, which we used earlier to exit a switch/case block. This becomes more interesting if you find yourself with nested loopsloops inside of loops. This is a common situation to be in. For example, you might want to loop through all the rows in a chessboard and, for each row, loop through each column. Calling break exits only one loop or switch/case, but you can use break 2 to exit two loops or switch/cases, or break 3 to exit three, and so on.

Was this article helpful?

0 0

Post a comment