Foreach
PHP Code Examples
[Download]   [Execute]
<?php $title = 'Foreach'; include '../head1.inc'; ?>
Here we are iterating over the values of an array.<p>
<?php 
$fred = array(4, 2, 1, 42, 11, -3, "Thud");
foreach($fred as $fd) 
	echo "$fd ";
echo "<p>\n";

$ct = 0;
foreach(array("This", "is", "the", "way", "to", "do", "it.") as $word) {
	echo "$word ";
	++$ct;
}
print " ($ct words)<p>\n";

echo "Here are all the <tt>_SERVER</tt> variables sent to the 
script:<p><table>";

foreach ($_SERVER as $key => $val) {
	echo "<tr><td>$key</td><td>", htmlspecialchars($val), "</td></tr>\n";
}
echo "</table>";
 ?>
</body>
</html>