PDA

View Full Version : newbie ?: foreach statment further understanding


bdd
08-11-2006, 12:52 AM
I'm into the part of my php book where it introduces arrays and the foreach loop statment.

Heres a example:

$days = range (1, 31);

echo '<select name="day">';
foreach ($days as $value) {
echo "<option value=\"$value\"> $value</option>\n";
}

I understand this script, the first part of the script echos HTML code to create the first drop-down menu with the '<select name="day">'; code. Thats where I get lost.

The book briefly explains the foreach statment in 3 sentences, not to which I comprehended.

I think it means, foreach (for each time) the $days array access a new value, it echos the below statment. Is this correct? Whats the "lamens term" for foreach, and how can you explain it in a sentence like for each blah blah blah. Thanks!

Brandoe85
08-11-2006, 12:55 AM
$days will be the numbers 1 - 31. The foreach is going to spit out each of the elements in $days.

Therefore, each time through the loop it'll display the next value, e.g:
first time through display $days[0], next time through $days[1]...until it reaches the end.


Check out php.net for the reference on foreach:
http://us2.php.net/foreach

Good luck;