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, if 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 the 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 bachelor’s degree. Please check out the following syntax to define the 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 the if statement.

Branching statement 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 use various types operators to write the condition in branching statements in PHP such as conditional operators and logical operators.

If else statement: –

Now we know about the if statement. In the if statement we can run a piece of code only if the condition is true, It means we don’t have any control in case of a false condition. To solve this problem we have the if-else statement. The if-else statement allows us to write code for both conditions true and false. Please check out the following syntax to define the 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 the else-if statement in case we have multiple conditions and we want to run a particular piece of code for different conditions. Please check out the following syntax to define the 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, Please follows 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 the 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.

Braching statements in PHP

Check the following example to understand the 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;
}
?>
Branching statement in PHP

Wooo hooo! Now that you’re familiar with branching statements in PHP, go ahead and use them in your daily coding exercises. Practice makes perfect, so make the most of these concepts in your programming routines. Happy coding!

Spread the love
Scroll to Top
×