In this tutorial, we will learn Branching Statements in PHP. Branching statements allow us to control the normal flow of the program. For example, you want to run a piece of code only if a certain condition would be true. Here is a list of branching statements in PHP.

  • if statements
  • if-else statement
  • else if statement
  • switch statement

Here we will learn some all the branching statements in detail.

if statement:

In case you want to run a piece of code only if a certain condition would be true. In this kind of situation, you can use if statement. For example, you are creating a web page for student admission where a student can only apply for admission if he/she already has done bachelors degree. Please check out the following syntax to define if statement in PHP.

if(condition){
  //body statement(s)
}

To understand the flow of the if statement. Please check out the following flow chart diagram of if statement.

Branching Statements in PHP

Check this example to understand this example clearly.

Example Program:

<?php
$bachelors="yes";
if($bachelors=="yes")
{
   echo "You can Apply for Admission";
}
?>

if else statement:

Now we know about if statement. In if statement we run a piece of code if the condition would be true but we do not any code if the condition would false. In the last example, we saw an example where a student can apply for admission if he/she has done bachelors but if a student not has done bachelors. For this kind of situation, we can use if-else statement where we can write code in else block in case condition would not true. Please check out following syntax to define if-else statement in PHP.

if(condition){
  //code in case condition is true
}else{
  //code in case condition is false
}

To understand the flow of the if-else statement. Please check out the following flow chart diagram.

Branching Statements in PHP

Example Program:

<?php
$bachelors="yes";
if($bachelors=="yes")
{
   echo "You can Apply for Admission";
}else{
  echo "You can not Apply for Admission, Try for other course.";
}
?>

else if statement:

We use else statement in case we have multiple conditions and we want to run a different piece of code for each condition. Please check out the following syntax to define else-if statement in PHP.

if(condition){
  //statements  if above condition is true
}else if(condition){
  //statements if above condition is true
}else if(condition){
  //statements if above condition is true
}else{
  //statements if all above conditions are false
}

To understand the flow of the else-if statement. Please check out the following flow chart diagram.

Branching Statements in PHP

To understand it clearly follow the given example program.

Example Program:

<?php
$marks=85;
if($marks-->=80){
   echo "Grade A";
}else if($marks>=60){
   echo "Grade B";
}else if($marks>=40){
   echo "Grade C";
}else{
   echo "Fail";
}
?>

switch statement:

We use the switch statement in case we have multiple options. for example, you have various courses and you want to open a different page for each course. Please check out the following syntax define switch statement in PHP.

switch(choice variable(value)){
  case 1:
    //statement(s) for case 1
    break;
  case 2:
    //statement(s) for case 2
    break;
  case n:
    //statement(s) for case n
    break;
  default:
    //statement(s) for default case
}

To understand the flow of the switch statement. Please check out the following flow chart diagram.

Branching statements in PHP

Check the following example to understand switch statement more clearly.

Example Program:

<?php
$choose=2;
switch($choose){
  case 1:
   echo "Red";
  break;
  case 2:
   echo "Blue";
  break;
  case 3:
   echo "Green";
  break;
  default:
   echo "Black";
  break;
}
?>