PHP/Statement/for loop
Содержание
- 1 A division table
- 2 A division table
- 3 A for loop in PHP
- 4 A for Loop That Divides 4000 by Ten Incremental Numbers
- 5 Alternating table row colors with for()
- 6 Fahrenheit and Celsius table by for loop
- 7 For loop: Counting Backwards
- 8 Counting Backwards
- 9 Counting By Fives
- 9.1 For loop format
- 9.2 For statement without all three statements
- 9.3 For statement without second condition
- 9.4 If statement inside a for loop
- 9.5 Iterating through a multidimensional array with for()
- 9.6 Iterating through a numeric array with for()
- 9.7 Loop goes from 0 to 100 in steps of 0.1
- 9.8 Loops Within Loops
- 9.9 Multiple expressions in for()
- 9.10 Nesting Two for Loops
- 9.11 Printing a <select> menu with for()
- 9.12 Printing out all the web-safe color codes
- 9.13 Specifying a number after break, such as break 2, to break out of two loops or switch/case statements
- 9.14 Use for loop to output HTML table
- 9.15 Using the for Statement
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
"); | $count_1 | "); print("
---|---|
$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 "
Element $i | $dinner[$i] |
?>
</source>
Fahrenheit and Celsius table by for loop
<source lang="html4strict">print "
Fahrenheit | Celsius |
---|---|
$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 "";
print ($x*$y);print " | ";
}
print "
?>
</body> </html>
</source>
<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>
<BODY>
The cost of $shares share/s is $cost #x0024s | ","\n";The 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>