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

130 lines
2.0 KiB
PHP
Raw Normal View History

<?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);
?>
2010-12-30 02:43:03 +00:00
<body>
<p>Using &lt;pre&gt; bgcolor = <?php echo $bgcolor; ?></p>
<!-- ======================================================= -->
2010-12-30 02:43:03 +00:00
<pre><?php
$c = 0;
while ($c < 5) {
print $c++ . "\n";
}
?></pre>
<!-- ======================================================= -->
2010-12-30 02:43:03 +00:00
<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>
<!-- ======================================================= -->
2010-12-30 02:43:03 +00:00
<pre><?php
$c = 6;
do {
print "Hi\n";
} while ($c < 5);
?></pre>
<!-- ======================================================= -->
2010-12-30 02:43:03 +00:00
<pre><?php
for ($i = 0; $i < 5; $i++) {
print "$i\n";
}
2010-12-30 02:43:03 +00:00
?></pre>
<!-- ======================================================= -->
2010-12-30 02:43:03 +00:00
<pre><?php
2010-12-30 02:43:03 +00:00
$menu = array(
"Toast and Jam",
"Bacon and Eggs",
"Homefries",
"Skillet",
"Milk and Cereal"
);
2010-12-30 02:43:03 +00:00
$count = count($menu);
2010-12-30 02:43:03 +00:00
for ($i = 0; $i < $count; $i++) {
$j = $i + 1;
2010-12-30 02:43:03 +00:00
print "$j. {$menu[$i]}\n";
}
2010-12-30 02:43:03 +00:00
?></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>
2010-12-30 02:43:03 +00:00
</body>
</html>