PHP loops allow you to run the same chunk of code repeatedly. PHP features while and do…while loops, which are handy for general-purpose looping, and the more specialized
for
loop, which is useful when you want to run a chunk of code a known number of times.
This article explains how PHP
for
loops work, and shows you some practical for
loop examples.
PHP for
loop syntax
PHP
for
loops have the following general syntax:for (
expression1;
expression2;
expression3) {
// This code is run as long as
expression2is true
}
// This code is run after the loop finishes
You can see that
for
loops are more complex than while loops — whereas a while
loop has 1 expression between the parentheses, a for
loop has 3 expressions, separated by semicolons.
Here’s how a
for
loop works:- The first time through the loop, the PHP engine evaluates expression1. This expression is known as the initialize, and is usually used to set up a counter variable for the loop.
- At the start of each iteration of the loop, expression2 is tested. If it’s
true
, the loop continues and the code inside the braces ({}) is run. If it’sfalse
the loop exits, and any code after the closing brace is run. expression2 is known as the loop test, and serves the same purpose as the single expression in awhile
loop. - After each iteration, the PHP engine evaluates expression3. This expression is known as the counting expression, and is usually used to change the counter variable.
Some for
loop examples
for
loops are primarily designed to run a chunk of code a known number of times. Here’s a simple example:<?php for ( $i = 1; $i <= 5; $i++ ) { echo "Counted to $i...<br />"; } echo "Finished!"; ?>
When run, this code displays the following:
Counted to 1... Counted to 2... Counted to 3... Counted to 4... Counted to 5... Finished!
You can see how the 3 expressions in a
for
loop make it easy to set up and increment a counter variable,$i
, for looping a known number of times.
You don’t have to count upwards though, as the following examples show:
<?php // Count backwards from 5 to 1 for ( $i = 5; $i >= 1; $i-- ) { echo "$i...<br />"; } echo "Finished!<br /><br />"; // Display all powers of 2 less than 40 for ( $num = 1; $num < 40; $num *= 2 ) { echo "$num...<br />"; } echo "Finished!<br />"; ?>
The above code displays the following:
5... 4... 3... 2... 1... Finished! 1... 2... 4... 8... 16... 32... Finished!
A real-world example: generating passwords
Say you want to generate an 8-character random password for a user. A
for
loop is perfect here, since you know in advance how many times you want the loop to run (8 in this case):<?php $password = ""; for ( $i = 0; $i < 8; $i++ ) { $password .= chr ( rand ( 0, 25 ) + 97 ); } echo "Your new password is: $password"; ?>
This displays something like:
Your new password is: fynqtfxs
for
loop expressions are optional
It’s worth pointing out that each of the 3 expressions in a
for
loop is optional. (If you miss out the loop test expression then it defaults to true
, making the loop continue forever.)
An example:
<?php $num1 = 1; $num2 = 5; for ( ; $num1 <= $num2; $num1++ ) { echo "$num1...<br />"; } echo "Finished!<br /><br />"; ?>
This code displays:
1... 2... 3... 4... 5... Finished!
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 />"; } ?>
Nesting Two for Loops
<?php <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> ?>
0 comments:
Post a Comment