Sometimes, while designing a program, you need to execute a set of statements repeatedly. You might need to display an entire set of data with hundreds of elements or list all numbers from 1 to 100. Fortunately, PHP offers loops, which allow you to execute a set of statements as many times as needed. There are several different types of loops. Each type has strengths and weaknesses. You most often find loops with arrays, which I go over in Chapter 10.
Let’s start off with the most common loop: while. Using the while loop, you execute the statements as long as a certain condition is true. Remember the three stages of the while loop:
Initialization outside the loop, which ensures the loop executes at least once
Condition inside the ...