The Foreach Loop! This loop is a very popular loop, and is used extensively in database-driven websites. The purpose of a Foreach Loop is to "loop through each key/value pair in an array" (w3schools).
The syntax looks like so:
foreach ($array as $value) { // execute code }
Basically, here's what happens during each loop iteration:
$value
$value
is echoed on the screenLet's use a real-world example of how to use a Foreach Loop:
// We need to create an array // Let's resurrect our array of moustaches $moustaches = array("Handlebar", "Salvador Dali", "Fu Manchu"); // Loop through the $moustaches array foreach ($moustaches as $moustache) { // Output each individual value echo "I love the $moustache
"; }
If coded correctly, this should echo the three moustaches in the array on your screen.
Feel free to experiment with the above Foreach Loop with some PHP using the practice.php page provided in this folder.