If Else Statement
×
Home Courses Community Support
If Else Statement

If else Statement statement used when we have another action to do if the condition is not true. If statement execute when the condition is true and else statement execute when the condition is not true.
 

For example, before going to office, Mr. Monu used to go temple on every Tuesday. Otherwise he got to office directly.

We will code for it like given below-

<?php
$day = "Fri";  //Fri stands for Friday
if ($day == "Tue"){
    echo "Mr. Monu will go to temple first.";
    }
    else{
    echo "Mr. Monu will go to office, not to temple.";
    }
?>

Output:

Mr. Monu will go to office, not to temple;

 

In above code, we set the value of day to Friday. Condition in above code is $day == "Tue" which is not true because it has been set to Friday. So the else statement executed.

If you want to use system time instead of setting the $day value, you can use date(D).

See below example-

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


It's output depends on your system date. Test this code on CodeLAB.

 

 

← Preview

If Statement

Next →

Nested If Else 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