Lecture 27: Foreach Loop
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:
Assign value of current array element to $value
$value is echoed on the screen
The array pointer then moves to the next element within the array
Repeat from beginning until final array element
Let'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.
DOWNLOAD COURSE FILES HERE
http://www.bradhussey.ca/download-php...
On this page of the site you can watch the video online Foreach Loop [#27] Code Dynamic Websites with PHP with a duration of hours minute second in good quality, which was uploaded by the user Brad Hussey 20 July 2014, share the link with friends and acquaintances, this video has already been watched 5,920 times on youtube and it was liked by 44 viewers. Enjoy your viewing!