PHP For Loop

This item was filled under [ PHP & MySql, References ]

The For loop is used to repeat executing a certain code for a specific amount of times. This could as well be done with the while loop however the for loop is made for this. The while loop may be used for much more complex stuff and for various other purposes. The for loop is sorely used for repeating.

for( … declaration …; … condition …; …action… ) {
	… execute code …
}

The arguments between brackets are 3: a declaration, a condition and an action. The declaration is usually an integer variable containing a start value, which is usually increased in the action. For example, we could make a for loop which is repeated 10 times:

for($i=1;$i<=10;$i++) {
	echo “<p>”.$i.”</p>”;
}

This will result in displaying the numbers 1 through 10.

Tagged with: [ , , , ]
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Comment