Articles → PHP → Conditional Statements in PHP

Conditional Statements in PHP
























Software requirement









Technical knowledge









What are conditional statements?













Types of conditional statements in PHP













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











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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250