Home > PHP > PHP Loops

PHP Loops

We are going to talk about all looping statements in PHP. We have four looping methods.


I. While loop
II. Do … While loop
III. For loop
IV. Foreach loop

Most of PHP statements are similar to C statements. So, a programmer who knows C, can learn PHP easily.

I. While Loop

While statement loops the operations while specified conditions are true. So, we can use this looping method when you don’t have or don’t know number of repetitions.

Structure 1.0 :

1
2
3
4
5
while( conditions ){
//…
//operations
//…
}

There is a simple code example that shows us how to use while loop in PHP.

Example 1.0 :

1
2
3
4
5
6
7
8
<?php
$k=0;
while($k<=5)
  {
  echo "The number : " . $k . "<br />";
  $k++;
  }
?>

Output of Example 1.0 :

1
2
3
4
5
6
Number : 0
Number : 1
Number : 2
Number : 3
Number : 4
Number : 5

II. Do … While Loop

With do…while loop, conditions are tested at the end of the loop. The loop is always executed at least once with this usage.

Structure 2.0:

1
2
3
4
5
do{
//…
//operations
//…
}while( conditions )

Example 2.0 :

1
2
3
4
5
6
7
<?php
$i=1;
do{
echo "The Number : " . ($i*4) . "<br/>";
$i=$i+1;
}while($i<4)
?>

Output of Example 2.0 :

1
2
3
Number : 4
Number : 8
Number : 12

III. For Loop

The for loop depends on a counter ( that increases or decreases by the same amount each time through the loop ) and loops the operations until the counter reaches a specified value.

Structure 3.0:

1
2
3
4
5
for(initialization; conditions; increment){
//...
//operations
//...
}

Example 3.0:

1
2
3
4
5
6
7
8
<?php
echo "The numbers: ";
for($i=0;$i<=10;$i++)
{
echo $i;
if($i!=10)echo(", ");
}
?>

Output of Example 3.0:

1
The numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

IV. Foreach Loop

For each … next statement is used for executing a block of operations for each element of a collection. There is no counter in for each … next loop but you have to declare an object variable.

Structure 4.0:

1
2
3
4
5
6
7
8
<?php
foreach($COLLECTION as $ITEM)
{/*

Operations

*/
}
?>

Example 4.0:

1
2
3
4
5
6
7
<?php
$my_array=Array("Ronald","Charlie","Richard");
foreach($my_array as $my_item)
{
echo $my_item. "<br/>";
}
?>

Output of Example 4.0:

1
2
3
Ronald
Charlie
Richard

V. Break and Continue Statements in PHP Loops


5.1 ) Break Statement

Break statement is used for exiting from the loop that contains the break statement immediately.

Example 5.0

1
2
3
4
5
6
7
<?php
echo "The numbers: ";
for($i=0;$i<=10;$i++){
echo $i." ";
if($i==6)break;
}
?>

Output of Example 5.0:

1
The numbers: 0 1 2 3 4 5 6

Example 5.1

1
2
3
4
5
6
7
<?php
echo "The numbers: ";
for($i=0;$i<=10;$i++){
if($i==6)break;
echo $i." ";
}
?>


Output of Example 5.1:

1
The numbers: 0 1 2 3 4 5

As you see in the Example 5.0 and Example 5.1 the loop is broken when the break statement is read.

5.2 ) Continue Statement

Continue statement is used for skipping from the loop that contains the continue statement immediately..

Example 5.2

1
2
3
4
5
6
7
8
<?php
echo "The numbers: ";
for($i=0;$i<=10;$i++)
{
if($i==7)continue;
echo $i." ";
}
?>

Output of Example 5.2:

1
The numbers: 0 1 2 3 4 5 6 8 9 10

In the example 5.2, when i reaches 7, operation is skipped and 7 is not written.

Bookmark and Share
Categories: PHP Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
eXTReMe Tracker