Conditional branching is required in programming when we need to execute some set of statements on satisfaction of a condition. PHP conditional Branching Structures are

  • if structure
  • if..else structure
  • if…elseif structure
  • switch-case structure

Simple If Structure

The Simple IF structure executes the block of statements if the condition is true. If the condition specified is false the statements after if block are executed

Syntax

if (condition)

{

                If block statements

}

Statement following if structure

Example

<?php
$empName='Amy';
if ($empName=='Amy')
{
      echo "Hello $empName! Please come<BR>";
}
echo "No one else is Allowed except $empName";
?>

Output:

if structure

Simple If…else Structure

The IF…ELSE  structure executes the if block statements if the condition is true. If the condition specified is false the statements of else block are executed.

Syntax

if (condition)

{

                if block statements

}

else

{

                else block statements

}

Statement following if structure

Example

<?php
$empName='Andy';
if ($empName=='Amy')
{
       echo "Hello $empName! Please come<BR>";
}
else
{
       echo "No one else is Allowed except $empName";
}
?>

Output:

if else structure

If…elseif structure

This structure is beneficial for nesting of IF structure to check more than one conditions.

Syntax

If (condition)

{

                If statement block

}

Elseif (condition)

{

                Else statement block

}

……

Else

{

                Last else block

}

Example

<?php
$day='Wednesday';
if ($day=='Monday')
{
      echo "Week just started! long to go<BR>";
}
elseif ($day=='Tuesday')
{
      echo "Just overcame Monday Blues";
}
elseif ($day=='Wednesday')
{
       echo "Half week past";
}
elseif ($day=='Thursday')
{
       echo "Just keep patience for another day";
}
elseif ($day=='Friday')
{
       echo "Thank God its Friday";
}
elseif ($day=='Saturday')
{
       echo "Where's the party toninght>";
}
else
{
       echo "Can't we have two sundays each week";
}
?>

Output:

if elseif structure

Switch-case structure

Among PHP conditional Branching Structures, switch case structure is a better option to if…elseif  structure. In a switch case structure the switch keyword is followed by the variable to be checked within parenthesis. The switch block contains various case statements followed by a possible value for the variable. The value of variable decides which statements will be executed. After each case statement block break; statement is given to stop checking of remaining case statements when one case block is executed

Syntax

Switch( Variable)

{

Case value1:

                Statements

                Break;

Case value2:

                Statements

Break;

………..

Default:

Break

}

Example

<?php
$day='Friday';
switch($day)
{
case 'Monday':
                echo "Week just started! long to go<BR>";
                break;
case 'Tueday':
                echo "Just overcame Monday Blues";
                break;
case 'Wednesday':
                echo "Half week past";
                break;
case 'Thursday':
                echo "Just keep patience for another day";
                break;
case 'Friday':
                echo "Thank God its Friday";
                break;
case 'Saturday':
                echo "Where's the party tonight>";
                break;
default:
                echo "Can't we have two Sundays each week";
                break;
}
?>

Output:

switch case structure