The foreach Statement

I must discuss the foreach statement here, although it is used with arrays, which are discussed in Chapter 5. An array is a collection of values referenced by keys. The foreach statement retrieves values from an array, one at a time. Like other looping structures, the foreach statement may have a simple or compound statement that's executed each time through the loop. Figure 3-6 shows the structure of a foreach statement.

Figure 3-6. The foreach statement.

The foreach statement expects an array, the keyword as, and a definition of the variables to receive each element. If a single value follows as, such as foreach($array ...

Get Core PHP Programming: Using PHP to Build Dynamic Web Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.