Branching Statements in PHP

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

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

Branching Statements in PHP


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 certain condition would be true. In this kind of situation you can use if statement. For example you are creating a webpage for student admission where a student can only apply for admission if he/she already has done bachelors degree. Check this example to understand this example clearly.

Example





if else statement: Now we know about if statement. In if statement we run a piece of code if condition would be true but we do not any code if condition would false. In last example we saw a example where a student can apply for admission if he/she has done bachelors but but if 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.

Example





else if statement: We use else statement in case we have multiple conditions and we want to run different piece of code for each condition. To understand it clearly follow given example.

Example

=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 switch statement in case we have multiple options. for example you have various courses and you want to open different page for each course. Check the follow example to understand switch statement more clearly.

Example







Share this tutorial with your friends on your social Media..

Branching Statements in PHP Branching Statements in PHP Branching Statements in PHP Branching Statements in PHP



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com