Looping in PHP

Repetitive tasks are always a burden to us. Deleting spam email, sealing 50 envelopes, and going to work are all examples of tasks that are repeated. The nice thing about programming is that you can avoid such repetitive tasks with a little bit of extra thinking. Most often these repetitive tasks are conquered in the loop.The idea of a loop is to do something over and over again until the task has been completed.

In PHP we have the following looping statements:

while - loops through a block of code if and as long as a specified condition is true

do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true

for - loops through a block of code a specified number of times

foreach - loops through a block of code for each element in an array

The while Statement

The while statement will execute a block of code if and as long as a condition is true.

Syntax

Code:

while (condition)
{
code to be executed;
}

Example

The following example demonstrates a loop that will continue to run as long as the variable i is less than, or equal to 5. i will increase by 1 each time the loop runs:

The for Statement

The for statement is used when you know how many times you want to execute a statement or a list of statements.

Syntax

Code:

for (initialization; condition; increment)
{
code to be executed;
}

The for statement has three parameters. The first parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop. If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.

In programming it is often necessary to repeat the same block of code a given number of times, or until a certain condition is met. This can be accomplished using looping statements. PHP has two major groups of looping statements: for and while. The For statements are best used when you want to perform a loop a specific number of times. The While statements are best used to perform a loop an undetermined number of times. In addition, you can use the Break and Continue statements within looping statements.
exemples:-

Sequence of instructions in an infinite loop the loop endlessly computer program, either due to cyclic non-termination conditions, one can never meet, or to cycle to start again. In the old cooperative multitasking operating system, an infinite loop is usually cause the entire system to stop responding. And now, the popular model of preemptive multitasking, infinite loops usually cause the program consumes all available processor time, but usually can be terminated by the user. Busy wait loop is sometimes mistakenly called "infinite loop. " One possible reason for a computer "freeze"is an infinite loop, the other including the deadlock and an access violation.

1. Set a counter variable to some initial value.
2. Check to see if the conditional statement is true.
3. Execute the code within the loop.
4. Increment a counter at the end of each iteration through the loop.

The for loop allows you to define these steps in one easy line of code. It may seem to have a strange form, so pay close attention to the syntax used!

Let us take the example from the while loop lesson and see how it could be done in a for loop. The basic structure of the for loop is as follows:
Pseudo PHP Code:

for ( initialize a counter; conditional statement; increment a counter)
{
do this code;
}

Notice how all the steps of the loop are taken care of in the for loop statement. Each step is separated by a semicolon: initiliaze counter, conditional statement, and the counter increment. A semicolon is needed because these are separate expressions. However, notice that a semicolon is not needed after the "increment counter" expression.