PHP while / do..while / for loop / foreach

by Yogesh Khanna 20-Jan-16

55 0 359

In programming, it is often essential to replicate the same block of code a given number of times, or until a definite state is met. This can be accomplished by using looping statements. PHP supports four major looping statements:

PHP have four looping types:

while - loop through a block of code if & as long as particular condition is true
do..while - loop through a block of code once, & then repeats the loop as long as the particular condition is true
for loop - loop through a block of code a specified number of times
foreach - loop through a block of code for each element in an array

While Loop Syntax

while(condition is true)
{
code to be executed;
}

Do While Loop Syntax

do
{
code to be executed;
}
while(condition is true);

For Loop Syntax

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

foreach Loop Syntax

foreach($array as $value)
{
code to be executed;
}

Share:

Comments

Leave a Reply