PHP - Loops
Wednesday, January 31st, 2007We use loops to repeat execution of block of code.
There are several loop types. Depending on situation, sometimes it is better to use one kind of loop than another, but every loop can be transformed into another one. This is not so important for beginners, you just need to know that there is good and better use of particular loop.
We have following loops:
for (*1; *2; *3)
{
// block
}
for loop is usually used when we know exactly how many times we need to execute repeating block of code.
for loop has three parameters. First, *1 is statement, and it is executed at the loop very start, and it often used for counter initialization. Second, *2 is a condition which is true or false. Condition is checked at every iteration start. If condition is true, execution will continue, if it is false execution will break. Third, *3 is statement and it is executed at the end of every loop iteration. It is usually used for counter increasing.
while (*)
{
// repeating block of code
}
