PHP/Statement/for loop

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

A division table

 
<?php
  $start_num = 1;
  $end_num = 10;
?>
<HTML>
<HEAD>
<TITLE>A division table</TITLE>
</HEAD>
<BODY>
<H2>A division table</H2>
<TABLE BORDER=1>
<?php
  print("<TR>");
  print("<TH> </TH>");
  for ($count_1 = $start_num;$count_1 <= $end_num;$count_1++)
    print("<TH>$count_1</TH>");
  print("</TR>");
  for ($count_1 = $start_num;$count_1 <= $end_num;$count_1++){
    print("<TR><TH>$count_1</TH>");
    for ($count_2 = $start_num;$count_2 <= $end_num;$count_2++){
        $result = $count_1 / $count_2;
        printf("<TD>%.3f</TD>",
               $result);  // see Chapter 108
      }
    print("</TR>\n");
  }
?> 
</TABLE>
</BODY>
</HTML>



A for loop in PHP

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



A for Loop That Divides 4000 by Ten Incremental Numbers

 
<html>
<head><title>A for Loop That Divides Ten Numbers</title></head>
<body>
<div>
<?php
    for ( $counter=1; $counter <= 10; $counter++ ) {
      $temp = 4000/$counter;
      print "4000 divided by $counter is.. $temp<br />";
    }
?>
</div>
</body>
</html>



Alternating table row colors with for()

 
<?
$row_color = array("red","green");
$dinner = array("A",
                "B",
                "C");
print "<table>\n";
for ($i = 0, $num_dishes = count($dinner); $i < $num_dishes; $i++) {
    print "<tr bgcolor="" . $row_color[$i % 2] . "">";
    print "<td>Element $i</td><td>$dinner[$i]</td></tr>\n";
}
print "</table>";
?>



Fahrenheit and Celsius table by for loop

 
print "<table>";
print "<tr><th>Fahrenheit</th><th>Celsius</th></tr>";
for ($fahr = -50; $fahr <= 50; $fahr += 5) {
    $celsius = ($fahr - 32) * 5 / 9;
    print "<tr><td>$fahr</td><td>$celsius</td></tr>";
}
print "</table>";



For loop: Counting Backwards

<html>
<head>
<title>
Counting Backwards
</title>
</head>
<body>
<h1>Counting Backwards</h1>
<?
for ($i = 10; $i > 0; $i--){
  print "$i <br>\n";
} // end for loop
?>

</body>
</html>



For loop: Counting By Fives

<html>
<head>
<title>
Counting By Fives
</title>
</head>
<body>
<h1>Counting By Fives</h1>
<?
for ($i = 5; $i <= 50; $i+= 5){
  print "$i <br>\n";
} // end for loop
?>
</body>
</html>



For loop format

<?
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<BR>");
}
?>



For statement without all three statements

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



For statement without second condition

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



If statement inside a for loop

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



Iterating through a multidimensional array with for()

 
<?
$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";
    }
}
?>



Iterating through a numeric array with for()

 
<?
$dinner = array("A",
                "B",
                "C");
for ($i = 0, $num_dishes = count($dinner); $i < $num_dishes; $i++) {
  print "Dish number $i is $dinner[$i]\n";
}
?>



Loop goes from 0 to 100 in steps of 0.1

 
<?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;
?>



Loops Within Loops

 
<?
    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";
                    }
            }
    }
?>



Multiple expressions in for()

 
<?
print "<select name="doughnuts">";for ($min = 1, $max = 10; $min < 50; $min += 10, $max += 10) {
    print "<option>$min - $max</option>\n";
}
print "</select>";
?>



Nesting Two for Loops

 
<html>
<head><title>Nesting Two for Loops</title></head>
<body>
<div>
<?php
    print "<table>";
    for ( $y=1; $y<=12; $y++ ) {
      print "<tr>";
      for ( $x=1; $x<=12; $x++ ) {
        print "<td>";
        print ($x*$y);
        print "</td>";
      }
      print "</tr>";
    }
    print "</table>";
?>
</div>
</body>
</html>



Printing a <select> menu with for()

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



Printing out all the web-safe color codes

 
<?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);
?>



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

 
<?
    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;
                    }
            }
    }
?>



Use for loop to output HTML table

 <HTML>
  <HEAD>
  <TITLE>Value of Shares</TITLE>
  <H3>Table Showing Cost of Shares</H3>
  <BODY>
  <TABLE BORDER=1>
  <?php
  for ($shares = 1; $shares <= 20; $shares++){
     $cost = $shares * 20;
     echo "<TR><TD>The cost of $shares share/s is $cost #x0024s</TD>","\n";
     $dividend = $cost * 0.10;
     echo "<TD>The dividend is $dividend #x0024s</TD></TR>  " ,"\n";
  }
  ?>
  </TABLE>
  </BODY>
  </HTML>



Using the for Statement

<html>
<head>
<title>Using the for Statement</title>
</head>
<body>
<?php
for ( $counter=1; $counter<=12; $counter++ ) {
    print "$counter times 2 is ".($counter*2)."<br>";
}
?>
</body>
 </html>