box-o-sand/docroot/html/loops.php

130 lines
2.0 KiB
PHP

<?php
require_once("_head.php");
$BGCOLOR_CHOICES = array(
'pink',
'purple',
'blue',
'red',
'green',
'gray',
'white',
'orange'
);
$bgcolor = $_GET['bgcolor'];
if (! $bgcolor) {
$bgcolor = $BGCOLOR_CHOICES[array_rand($BGCOLOR_CHOICES, 1)];
}
mkheader("looping!", $bgcolor);
?>
<body>
<p>Using &lt;pre&gt; bgcolor = <?php echo $bgcolor; ?></p>
<!-- ======================================================= -->
<pre><?php
$c = 0;
while ($c < 5) {
print $c++ . "\n";
}
?></pre>
<!-- ======================================================= -->
<pre><?php
$myname = "Fred";
$i = 0;
while ($myname != "Rumpelstiltskin") {
print $i++ . ": ";
if ($myname == "Fred") {
$myname = "Leslie";
} else {
$myname = "Rumpelstiltskin";
}
print "$myname\n";
}
print "How did you know?\n";
?></pre>
<!-- ======================================================= -->
<pre><?php
$c = 6;
do {
print "Hi\n";
} while ($c < 5);
?></pre>
<!-- ======================================================= -->
<pre><?php
for ($i = 0; $i < 5; $i++) {
print "$i\n";
}
?></pre>
<!-- ======================================================= -->
<pre><?php
$menu = array(
"Toast and Jam",
"Bacon and Eggs",
"Homefries",
"Skillet",
"Milk and Cereal"
);
$count = count($menu);
for ($i = 0; $i < $count; $i++) {
$j = $i + 1;
print "$j. {$menu[$i]}\n";
}
?></pre>
<!-- ======================================================= -->
<pre><?php
$array = array(
"1st" => "My House",
"2nd" => "My Car",
"3rd" => "My Lab"
);
foreach ($array as $key => $value) {
if ($value == "My Lab") {
$array[$key] = $value = "My Laboratory";
}
print "$key holds the value $value \n";
}
?></pre>
</body>
</html>