Navigation :
HomeSource code
Tutorials
RSS feeds
Articles
Wordpress plugins
Books
Sample Chapters
Software
Downloads
Hosting
manuals
Script directory
Training
Our Links
Site Sponsors :
Bookmark :
Links :
programmershelpwwwstores
dvd information
Downloads :
dzphp editorEasyPHP
mysql_phpgenerator
php-studio-trial
phpdesigner_7_2_5
phpMyAdmin-3.4.3.2
/phptriad2-2-1
phpxedit_321
rapidphp 2011
WampServer2.1d-x64
WampServer2.1e-x32
xampp-win32-1.7.4-VC6-installer
xampplite-win32-1.7.3
Misc :
Webmaster ResourcesOnly PHP
ScriptSearch.com
Scripts.com - Get the best scripts NOW!
AndreaPHP Programming
The for loop
The for loop is one of the basic looping structures in most modern programming languages. Like the while loop, for loops execute a given code block until a certain condition is met.
Syntax
The basic syntax of the for loop in PHP is similar to the c syntax:
for([initialization]; [condition]; [step])
Initialization happens the first time the loop is run. The Condition is evaluated at the top of the loop; if the condition is true, the loop runs (again), if it is false, the loop breaks and execution continues from the first line after the loop code block. Step occurs the second and each subsequent time the loop is run.
Example
for($i = 0; $i < 5; $i++)
{
echo($i . "<br />");
}
And the output:
0
1
2
3
4
Explanation
The variable $i is initialized as 0. When the loop ran once for the first time, it printed the original value of $i, 0. Each time the loop ran, it incremented $i, then checked to see if $i was still less than 5. If it was, it continued looping. When $i finally reached 5, it was no longer less than 5, so PHP stopped looping and went to the next line after the loop code block (none, in this case).
Using FOR loops to traverse arrays
In the section on while loops the sort() example uses a while loop to print out the contents of the array. Generally programmers use for loops for this kind of job.
Example
$menu = array("Toast and Jam", "Bacon and Eggs", "Homefries", "Skillet", "Milk and Cereal");
// note to self: get breakfast after writing this article
for($i = 0; $i < count($menu); $i++)
{
echo($i + 1 . ". " . $menu[$i] . "<br />");
}
Explanation
for($i = 0; $i < count($menu); $i++)
This line sets up the loop. It initializes the counter, $i, to 0 at the start, adds one every time the loop goes through, and checks that $i is less than the size of the array (hence the nice count function, which returns the size of an array).
{
echo($i + 1 . ". " . $menu[$i] . "<br />");
}
The echo statement is pretty self-explanatory, except perhaps the bit at the start, where we echo $i + 1. We do that because, as you may recall, arrays start at 0 and end at n - 1 (where n is their length), so to get a numbered list starting at one, we have to add one to the counter each time we print it.
Of course, as I mentioned before, both pieces of code produce the following output:
1. Toast and Jam
2. Bacon and Eggs
3. Homefries
4. Skillet
5. Milk and Cereal
Believe it or not, there's actually a way to traverse arrays that requires even less typing. (And isn't that the goal?) Check out the FOREACH loop for another way of doing what we did here.
All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks
Books :
Sponsors :
Random Code :
Get Flickr photos(internet)A daily MySQL row displayed(database)
gethostbyname function(functions)
Random Article :
PHP Account ActivationPHP Pear Packages Why they are so important to php developers
Network:
Programming resourcesTutorials directory
Hosting resources
ASP site
Domain names
Progged
Maxi directory
bigarticle : free articles
A Code
Code N Tutorials
Get PHP
Programmers help
Del.icio.us
Digg
Furl
Newsvine
Netscape
Reddit
StumbleUpon
Technorati
Squidoo
Windows Live
Yahoo MyWeb
Ask
Google

