If, elseif ... else and switch statements are used to make a decision based on a different situation.


You can use conditional statements in your code to make decisions. PHP supports following three decision-making statements -

  • if ... else statement: use this statement if you want to run a piece of code when a condition is true and another if the condition is false
  • elseif statement - used with the if ... else statement to execute a code snippet if one of several conditions is true
  • switch statement - used if you want to select one of several code blocks to be executed, use the switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.


The If...Else Statement

If you want to run a particular block of code if a condition is true and the other condition is false, use a statement if .... else.

Syntax

if (condition)
   code to be executed if condition is true;
else
   code to be executed if condition is false;

Example

The following example will print "Have a great weekend!" if the current day is a Friday, if not, it will print "Have a nice day!"

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

output:

Have a nice day!

The ElseIf Statement

If you want to execute some code if one of the various conditions are true, use the else if statement

Syntax

if (condition)
   code to be executed if condition is true;
elseif (condition)
   code to be executed if condition is true;
else
   code to be executed if condition is false;

Example

The following example will output "Have a nice weekend!" if today is Friday, and "Have a nice Sunday!" if today is Sunday. Otherwise, it will output "Have a nice day!" −

#example.php

<?php

//today
$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!";
}
?>

output:

Have a nice weekend!

The Switch Statement

If you want to select one of the many code blocks to be used, use the switch case statement.  The switch statement is used to avoid long blocks of if..elseif..else code.

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;
}


Example

date("D") return current day name. The switch statement is checking the condition if the condition is found it shows the current day

#test.php
<?php

$d = date("D");

switch ($d) {
    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 "Wonder which day is this ?";
}
?>

output:

Today is Thursday