All Loop Types in PHP

Loops are used to execute a block of codes until a certain condition is met. The main benefit of using a loop is to save the time and effort. This automates the repetitive tasks within a PHP program.There are four types of loops in PHP:-
  1. For Loop:- For loop execute a number of times until a specific counter is achieved. 
  2. While Loop:- while loop runs until a specific condition remains true.  
  3. Do …while Loop:- In do…while loop the block of code executed once after that condition is evaluated. If condition is true the block code executed until specific condition remains true. 
  4. Foreach Loop:- Foreach loop is used to execute a block of code for each element in an array.
 
Loops in PHP
PHP Loops

Read next blog post to get more about each loop.

Decision Making With Switch Statement in PHP

If you want execute one block of code out of many, use switch statement. The switch statement is to avoid long block of if..elseif..else code.
Below is switch statement syntax:-

switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}

Switch statement executes line by line and once finds a true condition it execute the corresponding block code and all other remaining cases till the end of switch statement.  To avoid this add a break statement at the end of each case block. The break statement tells the php to jump out of the switch case statement once it execute the code with first true case. If there is no matching case then the code in default block will be executed.

Difference between  if…else and switch case in PHP

The switch case statement is an alternative of if…elseif…else statement. The if…elseif…else statement also do same thing. The switch case statement finds a suitable block of code with true case and execute that code.

The only difference is that switch case statement is executed line by line and once a true condition is find not only executed that case but also execute all the subsequent cases till the end of switch statement. To prevent this break statement is used.

switch case in php
Switch Case Statement in PHP


Example:-

The following example will output current day.
 <?php
    $today = date("D");
    switch($today){
        case "Mon":
            echo "Today is Monday. ";
            break;
        case "Tue":
            echo "Today is Tuesday. ";
            break;
        case "Wed":
            echo "Today is Wednesday. ";
            break;
        case "Thu":
            echo "Today is Thursday.";
            break;
        case "Fri":
            echo "Today is Friday.";
            break;
        case "Sat":
            echo "Today is Saturday.";
            break;
        case "Sun":
            echo "Today is Sunday.";
            break;
        default:
            echo "This seems that some special day.";
            break;
    }
 ?>

ElseIf Statement in PHP - If More Than One Conditions is True


If there is a posibilities that several condition is true, then use elseif statement. Below is syntax for elseif statement in PHP:-

if (condition)
Code1 with posible true condition;
elseif (condition)
Code2 with posible true condition;
else
code with posible true condition;

Example:-

Below example will output “Have a nice weekend!” if the current day is Friday, and “Have a nice Sunday!” if current day is Sunday. Otherwise it will output “Have a nice day!”.

<html>
           <body>
          <?php
                    $d=date("D");
                    if ($d=="Fri")
                           echo "Have a nice weekend!";
                    elseif ($d=="Sun")
                           echo "Have a nice Sunday!";
                    else
                           echo "Have a nice day!";
          ?>
         </body>
</html>

ElseIf Statement in PHP
ElseIf Statement in PHP


Know About The If...Else Statement in PHP


If one condition is true and another is false then if else statement is used.

Below is syntax:-

If (Condition)
     Code with true condition;
Else
     Code with false condition;

Example:-

<html>
<body>
    <?php
       $d=date("D");
       if ($d=="Fri")
       echo "Have a nice weekend!";
       else
       echo "Have a nice day!";
    ?>
</body>
</html>

This example will output “Have a nice weekend!” if the current day is Friday, otherwise  it will output  “Have a nice day!”.

If...Else Statement
If...Else Statement

All About Decision Making Statements in PHP


If, elseif and switch statement are used for making decision based on different situations. 

Conditional statement can be used to make decisions. There are 3 decision making statements in PHP:-
  1. If…else statement:- If one condition is true and other is false then this statement is used.
  1. Elseif statement:- If there are more than one condition true this statement can be used.
  1. Switch statement:- This statement is used to select one block of code to execute out of many blocks. This is beneficial to avoid long blocks of if…else, elseif codes.

To read more about each click below links:-
  1. If…else statement
  1. Elseif statement
  1. Switch statement


 


Recent Post

All Loop Types in PHP

Loops are used to execute a block of codes until a certain condition is met. The main benefit of using a loop is to save the time and effor...

Popular Posts