www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons PHP for Loop Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
The for loop is the most advanced and the most frequently used of the loops in PHP. In it's simplest form, the for statement is used when we know how many times we want to execute a statement or a list of statements. Let's see its syntax:

for (init; condition; increment)
code to be executed;
The parameters of the for statement above are explained as under:
init: The is mostly used to set the loop counter, but could be any code to be executed once at the beginning of the loop statement.
condition: This is evaluated at the beginning of every loop iteration. If the condition evaluates to TRUE, the loop continues and the code executes. If it evaluates to FALSE, the execution of the loop ends.
increment: This is mostly used to increment a counter, but can be any code to be executed at the end of each loop iteration.

Any or all of the parameters can be omitted or there may be multiple expressions separated by commas. Please note that if we have multiple statements for the condition parameter, they all are evaluated but the result is taken from the last part. Leaving the condition parameter as empty means that the loop should be run indefinitely. Be careful when doing this, we must have some other sort of algorithm to end the loop e.g. by using a break statement inside the loop body.

The following example prints the text "Hello World!" five times:

for ($i=1; $i<=5; $i++)
echo "Hello World!<br />";

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.