Nested If Else Statement
×
Home Courses Community Support
Nested If Else Statement

Nasted if else statement used when we have more then two conditions. It is also called if else if statement.

Syntax for this statement is as below:

<?php
if(condition){
Code to execute if condition is true.
}
elseif(condition){
Code to execute if condition is true.
}
elseif(condition){
Code to execute if condition is true.
}
else{
code to be executed if no condition is true.
}
?>


To write the code let's assume that Mr. Monu has started to visit Lord Shiv temple on Monday and Lord Sai temple on Saturday, along with his visit to Lord Hanuman temple on Tuesday.

Below is the code for this situation-

<?php
$day = date("D");
if ($day == "Mon"){
	echo "Mr. Monu will go to Lord Shiv temple first.";
	}
	elseif ($day == "Tue"){
	echo "Mr. Monu will go to Lord Hanuman temple first.";
	}
	elseif ($day == "Sat"){
	echo "Mr. Monu will go to Lord Shai temple first.";
	}
	else{
	echo "Mr. Monu will go to office, not to temple.";
	}
?>

Output:

Output of this program depends on the date of your system. Copy paste this code in CodeLAB and check output.

 

 

← Preview

If Else Statement

Next →

Switch Statement

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App