PHP/Statement/for loop

Материал из Web эксперт
Перейти к: навигация, поиск

A division table

   <source lang="html4strict">

<?php

 $start_num = 1;
 $end_num = 10;

?> <HTML> <HEAD> <TITLE>A division table</TITLE> </HEAD> <BODY>

A division table

<?php print(""); print("");
 for ($count_1 = $start_num;$count_1 <= $end_num;$count_1++)
print(""); print(""); for ($count_1 = $start_num;$count_1 <= $end_num;$count_1++){ print("");
   for ($count_2 = $start_num;$count_2 <= $end_num;$count_2++){
       $result = $count_1 / $count_2;
printf("",
              $result);  // see Chapter 108
     }
print("\n"); } ?>
$count_1
$count_1%.3f

</BODY> </HTML>

 </source>
   
  


A for loop in PHP

   <source lang="html4strict">

<?php

           for ($i = 1; $i < 10; $i++) {
                   print "Number $i\n";
           }
   ?>
 
 </source>
   
  


A for Loop That Divides 4000 by Ten Incremental Numbers

   <source lang="html4strict">

<html> <head><title>A for Loop That Divides Ten Numbers</title></head> <body>

<?php

   for ( $counter=1; $counter <= 10; $counter++ ) {
     $temp = 4000/$counter;
     print "4000 divided by $counter is.. $temp
"; }

?>

</body> </html>

 </source>
   
  


Alternating table row colors with for()

   <source lang="html4strict">

<? $row_color = array("red","green"); $dinner = array("A",

               "B",
               "C");
print "\n"; for ($i = 0, $num_dishes = count($dinner); $i < $num_dishes; $i++) { print ""; print "\n";

}

print "
Element $i$dinner[$i]
";

?>

 </source>
   
  


Fahrenheit and Celsius table by for loop

   <source lang="html4strict">

print ""; print "";

for ($fahr = -50; $fahr <= 50; $fahr += 5) {

   $celsius = ($fahr - 32) * 5 / 9;
print "";

}

print "
FahrenheitCelsius
$fahr$celsius
";
 </source>
   
  


For loop: Counting Backwards

   <source lang="html4strict">

<html> <head> <title> Counting Backwards </title> </head> <body>

Counting Backwards

<? for ($i = 10; $i > 0; $i--){

 print "$i 
\n";

} // end for loop ?>

</body> </html>

      </source>
   
  


For loop: Counting By Fives

   <source lang="html4strict">

<html> <head> <title> Counting By Fives </title> </head> <body>

Counting By Fives

<? for ($i = 5; $i <= 50; $i+= 5){

 print "$i 
\n";

} // end for loop ?> </body> </html>

      </source>
   
  


For loop format

   <source lang="html4strict">

<? for ($x = 1, $y = 1, $z = 1; //initial expressions

    $y < 10, $z < 10;         // termination checks
    $x = $x + 1, $y = $y + 2, // loop-end expressions
    $z = $z + 3){
    
 print("$x, $y, $z
");

} ?>

      </source>
   
  


For statement without all three statements

   <source lang="html4strict">

<?php

  // Example Three
  $kilometers = 1;
  for (;;) {
     if ($kilometers > 5) break;
        echo "$kilometers kilometers = ".$kilometers*0.62140. " miles. 
"; $kilometers++; }

?>

      </source>
   
  


For statement without second condition

   <source lang="html4strict">

<?php

  // Example Two
  for ($kilometers = 1; ; $kilometers++) {
     if ($kilometers > 5) break;
        echo "$kilometers kilometers = ".$kilometers*0.62140. " miles. 
"; }

?>

      </source>
   
  


If statement inside a for loop

   <source lang="html4strict">

<?php

 $a = -3;
 for (; $a <= 20; $a++){
     if ($a == 0){
         break;
     }    
     $divide = 40/$a;
     echo "The result of dividing 40 by $a is $divide", "
"; }  ?> </source>


Iterating through a multidimensional array with for()

   <source lang="html4strict">

<? $specials = array( array("A", "A", "B"),

                  array("C","W", "P") );

for ($i = 0, $num_specials = count($specials); $i < $num_specials; $i++) {

   for ($m = 0, $num_sub = count($specials[$i]); $m < $num_sub; $m++) {
       print "Element [$i][$m] is " . $specials[$i][$m] . "\n";
   }

} ?>

 </source>
   
  


Iterating through a numeric array with for()

   <source lang="html4strict">

<? $dinner = array("A",

               "B",
               "C");

for ($i = 0, $num_dishes = count($dinner); $i < $num_dishes; $i++) {

 print "Dish number $i is $dinner[$i]\n";

} ?>

 </source>
   
  


Loop goes from 0 to 100 in steps of 0.1

   <source lang="html4strict">

<?php for ($i = 0; $i < 100; $i += 0.1) {

 if ($i == 50) echo "$i == 50" . "\n";
 if ($i >= 50 && $i < 50.1) echo "$i >= 50 && $i < 50.1" . "\n";

} echo $i; ?>

 </source>
   
  


Loops Within Loops

   <source lang="html4strict">

<?

   for ($i = 1; $i < 3; $i = $i + 1) {
           for ($j = 1; $j < 3; $j = $j + 1) {
                   for ($k = 1; $k < 3; $k = $k + 1) {
                           print "I: $i, J: $j, K: $k\n";
                   }
           }
   }

?>

 </source>
   
  


Multiple expressions in for()

   <source lang="html4strict">

<? print "<select name="doughnuts">";for ($min = 1, $max = 10; $min < 50; $min += 10, $max += 10) {

   print "<option>$min - $max</option>\n";

} print "</select>"; ?>

 </source>
   
  


Nesting Two for Loops

   <source lang="html4strict">

<html> <head><title>Nesting Two for Loops</title></head> <body>

<?php

print ""; for ( $y=1; $y<=12; $y++ ) { print ""; for ( $x=1; $x<=12; $x++ ) { print ""; } print ""; } print "
";
       print ($x*$y);
print "
";

?>

</body> </html>

 </source>
   
  


Printing a <select> menu with for()

   <source lang="html4strict">

<? print "<select name="people">"; for ($i = 1; $i <= 10; $i++) {

   print "<option>$i</option>";

} print "</select>"; ?>

 </source>
   
  


Printing out all the web-safe color codes

   <source lang="html4strict">

<?php for ($rr = 0; $rr <= 0xFF; $rr += 0x33)

   for ($gg = 0; $gg <= 0xFF; $gg += 0x33)
       for ($bb = 0; $bb <= 0xFF; $bb += 0x33)
           printf("%02X%02X%02X\n", $rr, $gg, $bb);

?>

 </source>
   
  


Specifying a number after break, such as break 2, to break out of two loops or switch/case statements

   <source lang="html4strict">

<?

   for ($i = 1; $i < 3; $i = $i + 1) {
           for ($j = 1; $j < 3; $j = $j + 1) {
                   for ($k = 1; $k < 3; $k = $k + 1) {
                           print "I: $i, J: $j, K: $k\n";
                           break 2;
                   }
           }
   }

?>

 </source>
   
  


Use for loop to output HTML table

   <source lang="html4strict">
<HTML>
 <HEAD>
 <TITLE>Value of Shares</TITLE>

Table Showing Cost of Shares

 <BODY>
<?php for ($shares = 1; $shares <= 20; $shares++){ $cost = $shares * 20; echo "","\n";
    $dividend = $cost * 0.10;
echo " " ,"\n";
 }
 ?>
The cost of $shares share/s is $cost #x0024sThe dividend is $dividend #x0024s
 </BODY>
 </HTML>
          
      </source>
   
  


Using the for Statement

   <source lang="html4strict">

<html> <head> <title>Using the for Statement</title> </head> <body> <?php for ( $counter=1; $counter<=12; $counter++ ) {

   print "$counter times 2 is ".($counter*2)."
";

} ?> </body>

</html>
          
      </source>