PHP loops

Loops in PHP is used to repeat the statement on behalf of some condition. Loops in PHP can be of two types entry level loop (condition is checked at entry point) and exit level loops (condition is checked at exit point). While loop in PHP and for loop in PHP used for iterating statements and syntax and example is as follows:

Syntax: while loop
[initialization]
while(condition)
{
// statement
[increment/decrement];
}
Syntax: for loop
for([initialization];condition;[Incr/decr])
{
//code
}

If you want to learn about do while loop in PHP, for loop in PHP, foreach loop in PHP then you can visit eitworld.com and learn PHP loops in easy and better way with examples.

Loops are used to execute the same code for a required number of times. They are also called Iterations. The code inside a loop keeps executing till a particular condition is satisfied. As soon as the condition becomes false, the loop is over and the code is transferred to the very next statement after the code of loop.

There are four loops in php-

1.while loop

2.do...while loop

3.for loop

4.foreach loop

1.While loop

In while loop, first the condition is checked. If the condition is true, then the control will enter into the loop. After executing the code inside the loop, it will again check the condition. This process will be continued until the condition becomes false.

Syntax

while(condition)

{

code to be executed many times

}


Example

<!DOCTYPE html>

<html>

<body>

<?php

$a=1;

while($a<5)

{

echo "Value of a is $a";

echo "<br>";

$a++;

}

?>

</body>

</html>


Output



2.Do...while Loop

In do...while, the condition is written at the end of the loop, so first the code inside the loop is executed and then the condition is checked for the second iteration. If the condition is true, the code of loop will be executed again, otherwise the control will leave the loop. Thus in do...while loop in any condition, the code is always executed at least once.

Syntax



do

{

code to be executed }while(condition);

Note that in do...while loop, there is a semicolon after the while statement.

Example



<!DOCTYPE html>

<html>

<body>

<?php

$a=1;

do

{

echo "Value of a is $a";

echo "<br>";

}while($a>1)

?>

</body>

</html>

Output



Here the condition is false even at the first time it is checked, but the code has been executed once before checking the condition.

3.For Loop

In for loop, you can initialize a counter, check the condition and increment or decrement the counter in a single statement.

Syntax

for(initialization; condition; increment/decrement)

{

code to be executed

}

The above for loop is executed in following order-

  • initialization- First of all the control goes to initialization. A counter for for loop is initialized at the very first. This part is executed only once
  • condition- After the initialization, the condition is checked
  • code- If the condition checked in step 2 is true, then the code inside the for loop is executed. If the condition was false, the control leaves the loop
  • increment/decrement- After execution of the code, the control comes to increment/decrement of the counter
  • condition- After incrementing or decrementing the counter, the control again checks the condition. If it is true, the code is executed otherwise loop is left

Example

<!DOCTYPE html>

<html>

<body>

<?php

for($a=1;$a<5;$a++)

{

echo "Value of a is $a";

echo "<br>";

}

?>

</body>

</html>

Output



4. Foreach Loop

This loop is used for arrays only. To run a loop for each value of an array, foreach loop is used

Syntax

foreach($arrayname as $values)

{

code to be executed }

The value of array at a particular iteration can be accessed using $values. It will be clear in following example-

Example

<!DOCTYPE html>

<html>

<body>

<?php

$myarray=array("Tom","John","Peter");

foreach($myarray as $names)

{

echo $names;

echo "<br>";

}

?>

</body>

</html>

Output