Tedd's code output for Loops:
Loops
Numeric "for loop": 123456789
String "for loop": abcdefghijklmnopqrstuvwxy
while "loop" : 123456789
foreach "loop": Sun Mon Tue Wen Thu Fri Sat
for "loop": Sun Mon Tue Wen Thu Fri Sat
foreach "loop" using a key: One/Apple | Two/Pear | Three/Peach | Four/Orange | Five/Apricot | Six/Pineapple |
CODE FOLLOWS
<?php
include('includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo("<h1>Tedd's code output for Loops:</h1>");
echo('<h2>Loops</h2>');
// Numeric 'for loop' =====================
echo('<p>Numeric "for loop": ');
for ($i = 1; $i < 10; $i++)
{
echo($i);
}
echo('</p>');
// String 'for loop' =====================
echo('<p>String "for loop": ');
for ($i = 'a'; $i < 'z'; $i++)
{
echo($i);
}
echo('</p>');
// while loop =====================
echo('<p>while "loop" : ');
$i = 1;
while ($i < 10)
{
echo($i);
$i++;
}
echo('</p>');
// foreach loop =====================
echo('<p>foreach "loop": ');
// populate a simple array
$a = array("Sun", "Mon", "Tue","Wen", "Thu", "Fri", "Sat");
foreach($a as $day)
{
echo(" $day ");
}
echo('</p>');
// for loop array =====================
echo('<p>for "loop": ');
for ($i = 0; $i < 7; $i++)
{
echo( $a[$i] . " " );
}
echo('</p>');
// foreach loop using key =====================
echo('<p>foreach "loop" using a key: ');
// populate an array with keys and values
$a = array('One' => "Apple",
'Two' => "Pear",
'Three' => "Peach",
'Four' => "Orange",
'Five' => "Apricot",
'Six' => "Pineapple"
);
foreach($a as $key => $fruit)
{
echo(" $key/$fruit |");
}
echo('</p>');
include('includes/footer.php');
?>