PHP Looping & Iteration

Looping & Iteration statements are used to execute the block of code repeatedly for a specified number of times or until it meets a specified condition. PHP supports four loop types.

The for loop in PHP

The for loop is used when we want to execute block of code known times.

Property Description
initialization Sets a counter variable
condition It test the each loop iteration for a condition. If it returns TRUE, the loop continues. If it returns FALSE, the loop ends.
increment Increment counter variable

The while loop in PHP

The while loop will execute a block of statement as long as a test expression is true.

The do…while loop in PHP

The do…while statement will execute a block of code and then test the condition for next iteration and executes next only if condition is true, block of code executes at least once.

The foreach Loop in PHP

The foreach statement is used to iterate over all elements of an array. It executes same piece of code for every element of an array