PHP Programming Loops

Spread the love

If you want to execute few lines of codes for a specific number of times repeatedly, then we will use PHP Programming Loops.

We are having PHP Programming Loops types:

  1. while
  2. do…while
  3. for
  4. foreach

There are two statements which are used in loops those are “continue” and “break“.

1. while loop:

The first type in PHP Programming Loops is “while” loop. The while loop executes some statements repeatedly for a particular number of times until given condition is true.

Syntax:

while(condition)
{
      statements
}

 

Example:

<?php
$i = 10;
while($i <= 100)
{
echo "This is ".$i;
$i = $i + 10;
}
?>

2. do..while loop:

The second type in PHP Programming Loops the “is do” while loop. The do..while loop is same as while loop but here condition is checked at the end of each iteration and not at the beginning.

Syntax:

do{
      statements
}while(condition);

Example:

$i = 1;

do
{
echo $i."<br>";
$i++
}while($i<=4);

Output:
1
2
3
4

3. for loop:

The third type in PHP Programming Loops is a “for” loop. A for loop is having three expressions, first initial statement second is a final condition on which loop depends and third is loop iterator.

Syntax 1:

for(start_stamnt; condition; iterator)
{
      statements
}

Example:

for($i=1;$i<=3;$i++)
{
echo $i."<br>";
}

Output:
1
2
3

Syntax 2:

for(start_stamnt; condition; iterator):
      statements
endfor;

Example:

for($i=1;$i<=3;$i++):

echo $i."<br>";

endfor:

Output:
1
2
3

4. foreach:

The fourth type in PHP Programming Loops is the “foreach” loop. foreach is used to iterate array values. This is special type of loop completly dedicated for iterating arrays.
There are two types of foreach loops.

Type 1: foreach with only values of an array

foreach($arrr as $val)
{
      echo “Value – “.$val;
}

Example:

$arr = array('name' => 'saurabh', 'emp_id' => '1234', 'subject' => 'PHP');

foreach($arr as $val)
{
echo $val."<br>";
}

Output:
saurabh
1234
PHP

Type 2: foreach with keys and values of an array

foreach($arr as $key=>$val)
{
      echo “Key – “.$key.” Value – “.$val;
}

Example:

$arr = array('name' => 'saurabh', 'emp_id' => '1234', 'subject' => 'PHP');

foreach($arr as $key => $val)
{
echo $key."=>".$val."<br>";
}

Output:
name=>saurabh
emp_id=>1234
subject=>PHP

 

break statement:

“break” statement is used to terminate the loop at any point of execution. If we use break statement inside the loop, which needs to be executed on the particular condition, then loop gets terminated of the condition becomes true.

Example:

<?php
for($i=0;$i<=10;$i++)
{
if($i == 5)
{
break;
}
echo "Value of i at loop break - ".$i;
}
?>

Output: Value of ‘i’ at loop break is -5

continue statement:

“continue” statement is used to skip the statement at specific condition inside a loop.

<?php
$a=1;

while($a < 5)
{
if($a == 2)
continue;
echo "Value of a is - ".$a;
$a++;
}
?>

Output:

Value of a is – 1
Value of a is – 3
Value of a is – 4


Spread the love

Leave a Comment