Source code for: challenge1_2.php

<html>
<head>
<title>Q7310 Challenge1.2 Looping with arrays</title>
</head>
<BODY bgcolor="000000"  link="#FFFF00" vlink="#FFFF00" alink="#00FF00"  text="#ececec">

<h1 align="center">
Program #2 <br> Processing Arrays with WHILE and FOR Loops
</h1>

<DIV align="center">
<FONT face="arial, helvetica, sans-serif" size=2 color="ececec">

<TABLE width="90%" border=1 cellpadding=10 cellspacing=0>
<TR>
<TD>

<?php

// INITIALIZE THE ARRAY, $testing
$testing = array(
         
"Program 1.2 ", "Purpose: ", "use ",
         
"WHILE ", "looping ", "to produce this sentence.", " "
         
);

// PRINT THE PHP-FUNCTION DETERMINED NUMBER OF ELEMENTS
print "The number of elements = ";
print
count($testing). " in this array.". "<br>"; //ALSO A DEBUG CODE


// SET THE MAXIMUM TIMES TO REPEAT: $times
// INITIALIZE THE ARRAY INDEX at 0 (element #1)
// REPEAT INSTRUCTIONS UNTIL INDEX < MAXIMUM REPEAT VALUE
// INCREMENT INDEX BY ONE
$times = count($testing);
$x = 0;
while (
$x < $times) {
print
$testing[$x];
++
$x;
}

?>

</TD>
</TR>
<TR>
  <TD>
<?php
//FOR LOOP - CONCISE VERSION OF WHILE LOOP
// SYNTAX for(initialize counter; test expression; increment value)
// followed by executable instructions within brackets {}

// INITIALIZE THE ARRAY INDEX
$character[0]= "Program 1.2's second purpose: ";
$character[1]= "use FOR looping to produce these sentences.  ";
$character[2]= "By using a concise syntax, ";
$character[3]= "FOR looping produces cleaner code, ";
$character[4]= "minimizes common errors, and produces ";
$character[5]= "equivalent results as the WHILE syntax loops .";

// SET THE MAXIMUM TIMES TO REPEAT: $times
$times = count($character);

// DEBUG CODE print $times." elements in this array.";
print "<br>";

// LOOPING
FOR ($x=0; $x < $times; ++$x) {
print
$character[$x];

// USE "IF STATEMENT" TO FORCE A LINE BREAK
IF ($x == 1){
print
"<br>"; //new line
}

}
print
"<br>". "This example includes a nested IF statement.";


?>
   </TD>
</TR>
<TR>
<TD>
SOURCE CODE: Click
<A HREF="http://rrchubbard.org/php/q7310/source1_2.php" target="_blank">
HERE </A>
</TD>
</TR>
</TABLE>
   
</FONT>
</DIV>
[<A href="top" target="_self">Top</A>]
<BR>
  Last Updated
<I>:
  <!-- #BeginDate format:fcAm1m -->Monday, September 27, 2004  21:39<!-- #EndDate -->
</I>



<P>
  <FONT face="Arial, Helvetica, sans-serif" size="1">
(c) 2004   Robin Y. Mabry Hubbard <A href="mailto:4ascii@marz.com">4ascii@marz.com</A>
  </FONT>
</P>

</body>
</html>

(c) 2004 Robin Y. Mabry Hubbard 4ascii@marz.com