There are four ways to make code loop in PHP so that you can perform repetitive actions, and each work in different ways. In practice, only three of the four are used regularly, but you need to at least be aware of all four.
The first loop type is called a while loop, and can be thought of as an if statement that is evaluated repeatedly until it fails. In pseudocode it looks like this:
while (condition is true) {
do code
}
Notice that again PHP uses code blocks to represent the extent of our loop - while loops start with an opening brace { and finish with a close brace } to tell PHP clearly which lines of code should be looped through.
Like if statements, you can put whatever conditions you like into while loops, however it's crucial that you change the value of the condition with each loop. Consider this piece of code:
<?php
while (1 == 1) {
// do stuff
}
?>
1 is always, always going to be equal to 1, which means this while loop will just whizz around in circles perpetually. This is called an infinite loop - a loop that will not exit because its condition will always be met - and they tend to drag your computer to a halt very quickly. Having said that, infinite loops can be useful - we'll look at those situations soon.
"While" loops are most often used to iterate over a list where there is no known limit to the number of iterations of the loop. For example:
<?php
while(there are still rows to read from a database) {
read in row;
move to the next to row;
}
?>
A more common form of loop is the "for" loop, and is slightly more complicated. A for loop has three parts - a declaration, a condition, and an action - as well as a loop counter variable that tracks the number of times the loop code has been executed (the number of iterations). The declaration is where the loop counter variable is declared and set to a starting value, the condition is where the loop variable counter is checked against a value, and the action is the action to take at the end of each iteration to change the loop counter.
Here is how a for loop looks in PHP:
<?php
for ($i = 1; $i < 10; $i = $i + 1) {
print "Number $i\n";
}
?>
As you can see, the for loop has the three distinct parts separated by a semi-colon each. Firstly, for our declaration, we set the variable $i to 1. Our condition, the second of the three parts in the for loop, is that the loop will execute if $i is less than 10. Finally, the action is that we add 1 to the value of $i every loop iteration - that is, every time the loop code is executed. So, a for loop is made up of a declaration, a condition, and an action.
Therefore, what this script does is to count from 1 to 10, outputting text along the way. Note that it will not actually output "Number 10" because we specify that $i must be less than 10, not less than or equal to it. Here is the output:
Number 1
Number 2
Number 3
Number 4
Number 5
Number 6
Number 7
Number 8
Number 9
The \n part at the end of the string is what causes the new lines, so if you're using Windows you'll need to use \r\n. This is all explained later - don't worry about it too much for now.
The third and least important loop type takes the form do...while, and is similar to a while loop with the difference that it is always executed at least once. Consider the following piece of code:
<?php
$foo = 12;
while ($foo < 10) {
do_bar();
}
?>
In that situation, the do_bar() function will never be called, because $foo will not be less than 10. However, in a do...while loop, we have this:
<?php
$foo = 12;
do {
do_bar();
} while ($foo < 10);
?>
In the latter case, do_bar() will be executed before $foo is compared against 10. If $foo is less than 10 when checked, the loop executes again. As you can see, the only difference between a while loop and a do...while loop is that the latter is always executed a minimum of once.
The last type of loop is the foreach loop, and is used to loop through an array of data. As arrays are quite unique in how they work, this particular type of loop is covered specifically in the Arrays chapter.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Infinite loops >>
Previous chapter: Case switching
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.