The if, if…else and if…elseif…else construct are one of the most important features of many languages including PHP, these conditional statements provides you different action for the different conditions.

IF Statement in PHP :

Syntax :

if(condition)
{
    Statements to be executed if condition is true;
}

Example :
<?php
$A =10;
If($A %2 ==0)
echo  “Number is Even”;
?>

IF…Else Statement in PHP :

Syntax :

if(condition)
{
      Statements to be executed if condition is true;
}
else
{
      Statements to be executed if condition is false;
}

Example :
<?php
$A =10;  $B=20;
If($A > $B)
echo  “A is Bigger than B”;
else
echo “B is Bigger than A”;
?>

If Else in PHP

IF…elseif…else Statement in PHP :
Syntax :

if(condition)
{
         Statements to be executed if condition is true;
}
elseif
{
        Statements to be executed if condition is true;
}
else
{
         Statements to be executed if condition is false;
}

Example :
<?php
$A =15;  $B=11;
If($A == $B)
echo  “A and B are Equal”;
elseif ($A > $B)
echo “A is Bigger than B”;
else
echo “B is Bigger than A”;
?>

If Elseif Else in PHP

Switch Statement in PHP:

The switch statement is similar to a series of IF statements on the same expression. Sometimes you want to compare the same expression or condition with many different values, and execute different piece of code depending on which value it equals to.

Syntax :

switch (expression)
{
     Case label 1 :
                    Code to be executed if expression = label 1;
                    break;
     Case label 2 :
                    Code to be executed if expression = label 2;
                    break;
       ....
       ....
       ....
      default :
                  Code to be executed if none of the label match with expression;
                  break;
}

Example :
<?php

$Day = 2;
switch ($Day)
{
    case 1 :   echo “Toady is Sunday”;
           break;
    case 2 :   echo “Toady is Monday”;
           break;
   case 3 :  echo “Toady is Tuesday”;
          break;
   ....
   ....

   default : echo “Invalid Day Number”;
          break;
}

?>

switch case in PHP