PHP Conditional Statements

php conditionals

Control Structures

In our everyday life, depending on different circumstances, our action changes – if it is raining, you may put on a raincoat instead of a t-shirt only; you may repeat a law of math until you memorize that etc, etc.

Similarly, in programming languages, control structures are structures that determine the action/flow of execution of the program. Control structures are blocks of statements. There are several control structures in php. These ares-

if else elseif / else if while
do-while for foreach break
continue switch declare return
Require Include require_once include_once
Goto ?:

In the following, you’ll learn the control structures used in php.

Conditional Statements

  1. In our usual life, our decisions change depending on the options we consider. For example, if it is raining, you’ll put on a rain coat or if it is hot today, you may put on a t-shirt.
  2. In programming languages, flow of the program is changed depending on the conditions. The constructs that control the program to make decision is called conditionals or conditional statements. Like real life, if a condition is true we take one or more actions; in programming if condition is true, one or more statements are executed. If the condition is false, then another statement or block of statements is executed.

In the following you’ll get to now different conditional statements used in php.

if statement

To make a decision and execute one or more statements, if-statement is used.

Syntax
if (expression)
statement;

  • The condition is used as expression in the parenthesis.
  •  If the expression is evaluate to boolean true, then the statement(s) after the parenthesis are executed.
  • If the expression is evaluated to boolean false then the statement is not executed.

Comparison between real life and if…statement

Real life If…Statement
If it is raining
-Put on a rain coat.
if($raining)
echo “Put on a rain coat”;

Example:

<?php
     $raining = “yes”;
     if($raining == “yes”)
         echo “Put on a rain coat.”;
 ?>

Output:
Put on a rain coat.

Explanation:

  1. In the above example, the value of the variable $raining is set to yes in line no 1.
  2. Then in line no 2, the equal operator (==) in the if-statement returns true as both operands are same.
  3. As it returns true, the program executes the statement after the parenthesis and prints “Put on a rain coat”.

There might have more than one statements that need to be executed if the if-statement is true. In this case, statements are enclosed by curly braces.

Example:

<?php
     $raining = “yes”;
     if($raining == “yes”){
         echo “Put on a rain coat. ”;
         echo “<br />”;
         echo “Go to school.”;
     }
 ?>

Output:
Put on a rain coat.
Go to school.

Alternative syntax for if-statement

  • PHP offers an alternative syntax for if-statement.
  • Here, instead of the opening brace, colon (:) is used and instead of closing brace, endif is used.

Syntax
if (expression):
statement;
endif;

Example

<?php
     $raining = “yes”;
     if($raining == “yes”):
         echo “Put on a rain coat.”;
     endif;
 ?>

Output:
Put on a rain coat.
Go to school.

else statement

Sometimes in real life, you may have alternate option if a condition doesn’t meet. For example, if it is raining, you’ll put on a rain coat; otherwise you’ll put on a t-shirt. In programming, else statement is used to represent this alternate option. In the if…statement when the expression doesn’t return true the program execute the else statement.

Syntax
if (expression)
statement;
else
statement;

When the expression in the if…statement evaluates to boolean false, then the statement after the parenthesis is not executed, rather the statement after else is executed.

Comparison between real life and if statement.

Real life If…else Statement
If it is raining
-Put on a rain coat.
or
-Put on a t-shirt
if($raining)
echo “Put on a rain coat”;
else
echo “Put on a t-shirt”;

Example:

<?php
     $raining = “no”;
     if($raining == “yes”)
         echo “Put on a rain coat.”;
     else
         echo “Put on a t-shirt.”;
 ?>

Output:
Put on a t-shirt.

Explanation:

  1. In the above example, the value of the variable $raining is set to “no” in line no 1.
  2. In line no 2, the equal operator (==) in the if-statement returns false as both operands are not same.
  3. So, the program executes the statement after the else and prints “Put on a t-shirt.”.

There might have more than one statements that need to be executed in the else statement. In this case, statements are enclosed by curly braces.

Example:

<?php
     $raining = “yes”;
     if($raining == “yes”){
         echo “Put on a rain coat. ”;
         echo “<br />”;
         echo “Go to school.”;
     }
     else{
         echo “Put on a t-shirt. ”;
         echo “<br />”;
         echo “Go to beach.”;
     }
 ?>

Output:
Put on a t-shirt.
Go to beach.

When the if…statement evaluates to true, then, the else statement is not executed.

Alternative syntax for else-statement

  • PHP offers an alternative syntax for else-statement.
  • Here, instead of the opening brace, colon (:) is used and instead of closing brace, endif is used.

Syntax
if (expression):
statement;
else:
statement;
endif;

Example

<?php
     $raining = “yes”;
     if($raining == “yes”):
         echo “Put on a rain coat. ”;
         echo “<br />”;
         echo “Go to school.”;
     else:
         echo “Put on a t-shirt. ”;
         echo “<br />”;
         echo “Go to beach.”;
     endif;
 ?>


Output:

Put on a rain coat.
Go to school.

elseif statement

Sometimes you may have more than two options to make a decision. For example, if it is Saturday, you’ll put on a black color shirt; if it is Sunday, you may put on a red color shirt. If it is Monday, you may put on a blue color shirt; otherwise you’ll put on a white color shirt. In programming, elseif statement is used to represent those alternate options in the middle.

Syntax
if (expression)
statement;
elseif (expression)
statement;
else
statement;

When the expression in the if…statement evaluates to boolean false, then the statement after the parenthesis is not executed, rather the statement after else is executed.

Comparison between real life and if statement.

Real life If…elseif…else Statement
If it is Saturday
-Put on a black color shirt.
if it is Sunday
-Put on a red color short
if it is Monday
-Put on a blue color shirt
or
-Put on a white color shirt
if(Saturday)
echo “Put on a black color shirt”;
elseif(Sunday)
echo “Put on a red color shirt”;
elseif(Monday)
echo “Put on a blue color shirt”;
else
echo “Put on a white color shirt”;

Example:

<?php
     $day = “Monday”;

     if($day == “Saturday”)
         echo “Put on a black color shirt.”;
     elseif($day == “Sunday”)
         echo “Put on a red color shirt.”;
     elseif($day == “Monday”)
         echo “Put on a blue color shirt.”;
     else
         echo “Put on a white color shirt.”;
 ?>

Output:
Put on a blue color shirt.”;

Explanation:

  1. In the above example, the value of the variable $day is set to  “Monday” in line no 1.
  2. In line no 2, the program test the condition in the if-statement. As the $day is not Saturday, it returns false.
  3. Then, it tests the next condition. As the $day is not Sunday also, it returns false.
  4. Then, it tests the next statement. Here, the expression returns true as the value of the variable $day is Monday.
  5. So the program prints the “Put on a blue color shirt”. When a condition becomes true, the program doesn’t execute the next conditions.

As said earlier, there might have more than one statements that need to be executed in the-if statement or in elseif statement or in else statement. In this case, statements are enclosed by curly braces.

Example:

<?php
     $day = “Monday”;

     if($day == “Saturday”){
         echo “Put on a black color shirt.”;
         echo “<br />”;
         echo “Go to shopping.”;
     }elseif($day == “Sunday”){
         echo “Put on a red color shirt.”;
         echo “<br />”;
         echo “Go to party.”;
     }elseif($day == “Monday”){
         echo “Put on a blue color shirt.”;
         echo “<br />”;
         echo “Go to coaching.”;
     }else{
         echo “Put on a white color shirt.”;
         echo “<br />”;
         echo “Go to office.”;
     }
 ?>

Output:
Put on a blue color shirt.
Go to coaching.

Alternative syntax for elseif-statement

  • PHP offers an alternative syntax for elseif-statement.
  • Here, instead of the opening brace, colon (:) is used and instead of closing brace, endif is used.

Syntax
if (expression):
statement;
elseif (expression):
statement;
else:
statement;
endif;

Example

<?php
     $day = “Monday”;

     if($day == “Saturday”):
         echo “Put on a black color shirt.”;
         echo “<br />”;
         echo “Go to shopping.”;
     elseif($day == “Sunday”):
         echo “Put on a red color shirt.”;
         echo “<br />”;
         echo “Go to party.”;
    else if($day == “Monday”):
         echo “Put on a blue color shirt.”;
         echo “<br />”;
         echo “Go to coaching.”;
     else:
         echo “Put on a white color shirt.”;
         echo “<br />”;
         echo “Go to office.”;
     endif;
 ?>


Output:

Put on a blue color shirt.
Go to coaching.

Switch statement

  • You can think of the switch statement like if…elseif…else statements.
  • You can use switch statement when you have to select one from lots of options.

Syntax
switch (expression) {
case label:
statement(s);
break;
case label:
statement(s);
break;
….
default:
statement(s);
}

  • Label must be either a number or a string.
  • Each label ends with a colon.
  • There may be one or more statements after the case label.
  • break is used to terminate from the switch. If break is not used the program continue executing until it gets it reaches a break statement.
  • The default statement is similar to else statement in if else statement.

Instead of using curly brackets inside the statements after each case statement, we use starts with colon and end with break.

How it works

  1. The value of the expression after the switch is matched against the labels.
  2. After a match is found the subsequent statements for that case are executed until it reaches the break statement.
  3. If the break statement is not found, it continues executing the following statements until it reaches the next break statement.
  4. The default statement is optional and executes if no match found in any label.

Example:
Comparison between real life and switch statement.

Real life Switch statement
If it is Saturday
-Put on a black color shirt.
if it is Sunday
-Put on a red color short
if it is Monday
-Put on a blue color shirt
or
-Put on a white color shirt
switch ($day) {
case “Saturday”:
echo “Put on a black color shirt.”;
break;
case “Sunday”:
echo “Put on a red color shirt.”;
break;
case “Monday”:
echo “Put on a blue color shirt.”;
break;
default:
echo “Put on a white color shirt.”;
}

<?php
$day = “Monday”;
     switch ($day) {
     case “Saturday”:
        echo “Put on a black color shirt.”;
        break;
     case “Sunday”:
        echo “Put on a red color shirt.”;
        break;
     case “Monday”:
        echo “Put on a blue color shirt.”;
        break;
     default:
       echo “Put on a white color shirt.”;
 }
 ?>

Output:
Put on a blue color shirt.;

 

Explanation:

  1. In the above example, the value of the variable $day is set to Monday.
  2. The value of the variable $day in the parenthesis after the switch is Monday.
  3. The program will look for Monday in all the case statements in the label position serially from beginning.
  4. The value of the first label position is Saturday; so the program passes it.
  5. The value of the second label position is Sunday; so the program passes it also.
  6. The program finds the match in the third label position; so, it executes its subsequent statement:”Put on a blue color shirt.”
  7. Then the program reaches the break keyword and exits from the switch statement.

Alternative syntax for switch-statement

  • PHP offers an alternative syntax for switch-statement.
  • Here, instead of the opening brace, colon (:) is used and instead of closing brace, endswitch is used.

Syntax
Switch (expression):
case label:
statement(s);
break;
case label:
statement(s);
break;
….
default:
statement(s);
endswitch;

Example

<?php

$day = “Monday”;

     switch ($day):
     case “Saturday”:
        echo “Put on a black color shirt.”;
        break;
     case “Sunday”:
        echo “Put on a red color shirt.”;
         break;
     case “Monday”:
        echo “Put on a blue color shirt.”;
         break;
     default:
       echo “Put on a white color shirt.”;
     endswitch;
 ?>

Output:
Put on a blue color shirt.;

Ternary operator

  • Ternary operator is the shorthand version of if-else statement.

Syntax:
Expression ? Execute if true : execute if false

  • As you can see in the above syntax, It takes three operands.

How it works

  1. First the program tests whether the expression returns true or false.
  2. If it returns true, then the statement before the colon (:) is executed.
  3. If it returns false, then the statement after the colon (:) is executed.

Example:
Comparison between if-else and ternary

If…else Ternary operator
if(raining)
-Put on a rain coat
else
-Put on a t-shirt
$raining ? “Put on a rain coat” : “Put on a rain coat;”

Example:

<?php
     $raining = "no";
     echo $raining=="yes" ? 'Put on a rain coat.' : 'Put on a t-shirt.';
 ?>

Output:
Put on a t-shirt.

Explanation:

  1. The value of the variable $raining is set to “no” in line no 1
  2. In line no 2, as the value of the variable $raining doesn’t match with “yes”, the expression part ($raining==”yes”) returns false.
  3. As the expression returns false, the statement after the colon is executed.
  4. And the echo at the beginning of the line print “Put on a t-shirt.” To the screen

You can catch the return value in a variable too. Check the following example.

<?php
     $raining = "no";
     $result =  $raining=="yes" ? "Put on a rain coat.": "Put on a t-shirt.";
 ?>
‹‹ PHP Operators – Part2 : Previous Lesson Next Lesson: PHP Loops ››