HomePHP Page 7 - PHP 101 (part 3) - Chocolate Fudge And Time Machines
What's That Noise? - PHP
After teaching you the fundamentals of form processing, PHP 101 returns with an explanation of WHILE, FOR and FOREACH loops, those PHP constructs that can save you hours of unnecessary HTML coding. Also included: array variables, the auto-increment operator, and some yummy desserts.
Of course, there is a simpler way of extracting all the elements of an array - PHP4 has a "foreach" loop designed specifically for this purpose and similar in syntax to the Perl construct of the same name. Here's what it looks like:
foreach ($array as $temp)
or, in English, "take each element of the array variable $array, place it in
the variable $temp, and execute the set of statements within the curly braces on $temp"
Let's demonstrate this by re-writing the example above in terms of the "foreach" loop:
define some array variables
$then = array("The Beatles", "Roy Orbison", "Frank
$now = array("Britney Spears", "N-Sync", "Jennifer Lopez", "Blink
Mom and Dad listen to
// use loop to extract and display
($then as $artist)
echo $artist . ", ";
the living room, I'm grooving
foreach ($now as $artist)
. ", ";
in the basement!