In this article, I will explain different types of loops in PHP with examples.Loops in PHP are used to execute the same block of code for a specified number of times. PHP supports following four loop types.

for - loops through a block of code a specified number of times.

<?php  
for ($qa_var=1;$qa_var<=5;$qa_var++) {
  echo "The number is: $qa_var <br>";
}
?> 

Output:

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

In the above example, $qa_var=1 set as 1 at starting position after running for loop code $qa_var value is increased by 1 and finally checking this value less than or equals to 5.


while - loops through a block of code if and as long as a specified condition is true.

<?php 
$qa_var = 1; 
while($qa_var <=10) {
    echo "The number is: $qa_var <br>";
    $qa_var++;
} 
?>

Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10

The example above first sets a variable $qa_var to 1 ($qa_var = 1). Then, the even as loop will retain to run as long as $qa_var is much less than, or identical to 10 ($qa_var <= 10). $qa_var will increase by 1 on every time the loop runs ($qa_var++)

do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true.

<?php
$qa_var = 1;
do{
    $qa_var++;
    echo "The number is " . $qa_var . "<br>";
}
while($qa_var <= 5);
?>

Output:
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6

In the above example define a loop that starts with $qa_var=1. It will then increase $qa_var with 1, and print the output. Then the condition is evaluated, and the loop will continue to run as long as $qa_var is less than, or equal to 5.


foreach - loops through a block of code for each element in an array.

<?php  
$qa_numbers = array("one", "two","three","four", "five"); 
foreach ($qa_numbers as $qa_value) {
  echo "$qa_value <br>";
}
?>  

Output:
one
two
three
four
five

Break statement in PHP

break ends execution of the current for, foreach, while, do-while or switch structure.

example:

<?php
$qa_var = 0;
for ($qa_var = 0;$qa_var <= 10;$qa_var++){
    if ($qa_var==7){
        break;
    }
    echo $qa_var;
    echo "<br />";
}
echo "Loop End" ;
?>

Output :
0
1
2
3
4
5
6

Continue statement in php
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

<?php 
$qa_var = 0;
for ($qa_var = 0;$qa_var <= 10;$qa_var++){
    if ($qa_var==5){
        continue;
    }
    echo $qa_var;
    echo "<br />";
}
echo "Loop End" ;
?>

Output:
0
1
2
3
4
6
7
8
9
10

Switch Statement:
It is used to perform different actions based on different conditions.

<?php
$fav_hero = "Amitabh Bachchan";
switch ($fav_hero) {
    case "Amitabh Bachchan":
        echo "Your favorite hero is Amitabh Bachchan!";
        break;
    case "Shahrukh Khan":
        echo "Your favorite hero is Shahrukh Khan!";
        break;
    case "Aamir Khan":
        echo "Your favorite hero is Aamir Khan!";
        break;
    default:
        echo "Your favorite hero is neither Amitabh Bachchan, Shahrukh Khan, nor Aamir Khan!";
}
?>

Output :

Your favorite hero is Amitabh Bachchan!

In the above example First we have a single expression n , that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.

The if Statement
It is used to execute a block of code only if the specified condition evaluates to true.
example:

<?php
$qa_var1=20;
$qa_var2=10;
if ($qa_var1 > $qa_var2)
  echo $qa_var1." is bigger than ".$qa_var2;
?>

Output:
20 is bigger than 10

In the above example condition comparing value variable #qa_var1 with $qa_var2 if the condition is true we got the output like above.

The if...else Statement

The if...else statement allows you to execute one block of code if the specified condition evaluates to true and another block of code i.e after else, if evaluates to false.

example:

<?php
$qa_var1=3;
if ($qa_var1%2==0){
  echo "Given Number is Even";
}else{
echo "Given Number is Odd";
?>

Output :
Given Number is Odd.
In the above example, $qa_var1 is 3.Modulo operation with 2 for this we got result 1.
here condition is false that's why we got output from the else block.

if qa_var1 value equals to 2 if the condition is true we got the output as "Given Number is Even" for if block.

The if...elseif...else Statement
The if...elseif...else a special statement that is used to combine multiple if...else statements.

<?php
$qa_day = date("D");
if($qa_day == "Mon"){
    echo "Today is Monday!";
} 
elseif($qa_day == "Tue"){
    echo "Today is Tuesday!";
} 
elseif($qa_day == "Wed"){
    echo "Today is Wednesday!";
} 
elseif($qa_day == "Thu"){
   echo "Today is Thursday!";
} 
elseif($qa_day == "Fri"){
   echo "Today is Friday!";
} 
elseif($qa_day == "Sat"){
   echo "Today is Saturday!";
} 
else{
    echo "Today is Sunday!";
}
?>

in the above example based on the current date we will get the output.
for example, today is "Wednesday" we get Wed value from $qa_day value so in above example, based on 3rd condition we will get the output like this "Today is Wednesday!"

The Ternary Operator:

The ternary operator is represented by the question mark (?) symbol and it takes three operands: a condition to check, a result for true, and a result for false.It is a shorthand way of writing the if...else statements.

Example:

<?php 
$qa_gender='m';
echo $qa_gender=='m'?'Male':'Female';
?>

Output :
Male

Code in if-else condition for above example would be

<?php 
$qa_gender='m';
if($qa_gender=='m'){
echo 'Male';
}
else{
echo 'Female';
}
echo $qa_gender=='m'?'Male':'Female';
?>

Output:
Male

Ternary Operator and if-else conditions both are same.But Ternary Operator is simple and have less code when compared to if-else condition