Articles → PHP → Conditional Statements In PHP

Conditional Statements In PHP






Software Requirement




  1. XAMPP is installed on your machine.
  2. Any text editor like Notepad

Technical Knowledge




  1. How PHP works?
  2. Basic knowledge of any programming language will help.

What Are Conditional Statements?




  1. If there is no client call in the evening (Condition), I will go for movie (Action).
  2. If you want to play games (Condition) then we will go to gaming zone (Action) otherwise we will go home (Action if condition is not met).
  3. If air tickets are cheap (Condition) then I will go by flight (Action) or if train tickets are available (Condition) then I will go by train (Action) else I will hire a cab (Action if both the Conditions are not met).



Types Of Conditional Statements In PHP




  1. If statement – executes some code if certain condition is true.
  2. If..else statement – Execute some code if condition is true or execute some other code if condition is false.
  3. If..elseif..else statement – Executes some code based on which condition is true.
  4. Switch statement – Similar to if..elseif..else statement switch statement executes some code based on which condition is true. There is a difference between if..else and switch statement which will be covered in last section.

If Statement




if (condition) {
  code to be executed if condition is true;
}




<html>
  <body>
<?php
$is_client_call = True;

if ($is_client_call == True)
{
    echo "I will go for movie";
}
?>
  </body>
</html>





If..Else Statement




if (condition) {
  code to be executed if condition is true;
} else {
  code to be executed if condition is false;
}




<html>
  <body>
<?php
$wish_to_play_game = True;

if ($wish_to_play_game == True)
{
    echo "Go to gaming zone";
}
else
{
    echo "Go Home";
}
?>
  </body>
</html>





If..Elseif..Else Statement




if (condition) {
  code to be executed if condition is true;
} elseif (condition) {
  code to be executed if condition is true;
} else {
  code to be executed if condition is false;
}




<html>
  <body>
<?php
$cheap_air_tickets = False;
$train_tickets_available = True;

if ($cheap_air_tickets == True)
{
    echo "Go by flight";
}
elseif ($train_tickets_available == True)
{
    echo "Go by train";
}
else
{
    echo "Hire a cab";
}
?>
  </body>
</html>





Switch Statement




switch (n) {
  case label1:
    code to be executed if n=label1;
    break;
  case label2:
    code to be executed if n=label2;
    break;
  case label3:
    code to be executed if n=label3;
    break;
  ...
  default:
    code to be executed if n is different from all labels;
}




<html>
  <body>
<?php
$mode = "TRAIN";

switch ($mode)
{
    case "FLIGHT":
        echo " I will go by flight";
    break;
    case "CAB":
        echo " I will go by Cab";
    break;
    default:
        echo " I will go by Train";
}
?>
  </body>
</html>





Difference Between If..Else And Switch Statement




  1. In case of switch statement we cannot apply logical operators like less than, greater then etc.
  2. Switch statement can only contains constant values.
  3. Switch statement is quicker in execution as compared to if..else.



Posted By  -  Karan Gupta
 
Posted On  -  Thursday, August 28, 2014

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250