PHP loop
PHP loop used to execute same block of code again and again specified number of time.
Loops execute block of code while the loop condition is true.
In PHP supports four types of loop.
- for loop
- while loop
- do-while loop
- foreach loop
PHP for loop
for loop – In for loop execute block of code up to the condition is true.
In for loop condition is predefined.
for loop syntax
for ( initialization; condition; increment/decrements)
{
// block of code to be executed
}
PHP while loop
PHP while loop – In while loop the code of block execute while the specified condition is true.
While loop syntax
while(condition)
{
//block of code to be executed
}
PHP do-while loop
In do-while loop always execute block of code at least once, then check condition and repeat the loop as long as the condition is true.
do-while loop syntax
do
{
// block of code to be executed
}
while (condition)
PHP foreach loop
In foreach loop used in on array.
PHP foreach syntax
foreach (array as value)
{
//bock of code to be executed
}