Branching Statements in PHP

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

Available Branching Statements in PHP:

  • If statements
  • If-else statement
  • Else if statement
  • Switch statement

Now we will learn about 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 a course 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 the following example program to understand this if statement clearly.

Example Program:

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

We can write use various operators to write the condition in branching statements in PHP such as conditional operators and logical operators.

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 in the case of if-statement, we do not have 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 already done bachelors but if a student has not done bachelors. For this kind of situation, we can use the if-else statement where we can write code for both situations true and false. 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-if statement in case we have multiple conditions and we want to run a different piece of code for different the 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 to 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;
}
?>
Spread the love