What are the different types of selection statements in php?


Conditional statements are used to perform different actions based on different conditions.


PHP Conditional Statements

Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

  • if statement - executes some code if one condition is true
  • if...else statement - executes some code if a condition is true and another code if that condition is false
  • if...elseif...else statement - executes different codes for more than two conditions
  • switch statement - selects one of many blocks of code to be executed

PHP - The if Statement

The if statement executes some code if one condition is true.

Syntax

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

Example

Output "Have a good day!" if the current time (HOUR) is less than 20:

<?php
$t = date("H");

if ($t < "20") {
  echo "Have a good day!";
}
?>

Try it Yourself »



PHP - The if...else Statement

The if...else statement executes some code if a condition is true and another code if that condition is false.

Syntax

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

Example

Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:

<?php
$t = date("H");

if ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

Try it Yourself »


PHP - The if...elseif...else Statement

The if...elseif...else statement executes different codes for more than two conditions.

Syntax

if (condition) {
  code to be executed if this condition is true;
} elseif (condition) {
  code to be executed if first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}

Example

Output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":

<?php
$t = date("H");

if ($t < "10") {
  echo "Have a good morning!";
} elseif ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

Try it Yourself »


PHP - The switch Statement

The switch statement will be explained in the next chapter.


PHP Exercises



View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    PHP allows us to perform actions based on some type of conditions that may be logical or comparative. Based on the result of these conditions i.e., either TRUE or FALSE, an action would be performed as asked by the user. It’s just like a two- way path. If you want something then go this way or else turn that way. To use this feature, PHP provides us with four conditional statements:

    • if statement
    • if…else statement
    • if…elseif…else statement
    • switch statement

    Let us now look at each one of these in details:

    1. if Statement: This statement allows us to set a condition. On being TRUE, the following block of code enclosed within the if clause will be executed.

      Syntax :

      if (condition){
          // if TRUE then execute this code
      }
      

      Example:

      <?php

      $x = 12;

      if ($x > 0) {

          echo "The number is positive";

      }

      ?>

      Output:

      The number is positive
      

      Flowchart:

      What are the different types of selection statements in php?

    2. if…else Statement: We understood that if a condition will hold i.e., TRUE, then the block of code within if will be executed. But what if the condition is not TRUE and we want to perform an action? This is where else comes into play. If a condition is TRUE then if block gets executed, otherwise else block gets executed.

      Syntax:

      if (condition) {
          // if TRUE then execute this code
      }
      else{
          // if FALSE then execute this code
      }
      

      Example:

      <?php

      $x = -12;

      if ($x > 0) {

          echo "The number is positive";

      }

      else{

          echo "The number is negative";

      }

      ?>

      Output:

      The number is negative
      

      Flowchart:

      What are the different types of selection statements in php?

    3. if…elseif…else Statement: This allows us to use multiple if…else statements. We use this when there are multiple conditions of TRUE cases.
      Syntax:
      if (condition) {
          // if TRUE then execute this code
      }
      elseif {
          // if TRUE then execute this code
      }
      elseif {
          // if TRUE then execute this code
      }
      else {
          // if FALSE then execute this code
      }
      

      Example:

      <?php

      $x = "August";

      if ($x == "January") {

          echo "Happy Republic Day";

      }

      elseif ($x == "August") {

          echo "Happy Independence Day!!!";

      }

      else{

          echo "Nothing to show";

      }

      ?>

      Output:

      Happy Independence Day!!!
      

      Flowchart:

      What are the different types of selection statements in php?

    4. switch Statement: The “switch” performs in various cases i.e., it has various cases to which it matches the condition and appropriately executes a particular case block. It first evaluates an expression and then compares with the values of each case. If a case matches then the same case is executed. To use switch, we need to get familiar with two different keywords namely, break and default.
      1. The break statement is used to stop the automatic control flow into the next cases and exit from the switch case.
      2. The default statement contains the code that would execute if none of the cases match.

      Syntax:

      switch(n) {
          case statement1:
              code to be executed if n==statement1;
              break;
          case statement2:
              code to be executed if n==statement2;
              break;
          case statement3:
              code to be executed if n==statement3;
              break;
          case statement4:
              code to be executed if n==statement4;
              break;
          ......
          default:
              code to be executed if n != any case;
      
      

      Example:

      <?php

      $n = "February";

      switch($n) {

          case "January":

              echo "Its January";

              break;

          case "February":

              echo "Its February";

              break;

          case "March":

              echo "Its March";

              break;

          case "April":

              echo "Its April";

              break;

          case "May":

              echo "Its May";

              break;

          case "June":

              echo "Its June";

              break;

          case "July":

              echo "Its July";

              break;

          case "August":

              echo "Its August";

              break;

          case "September":

              echo "Its September";

              break;

          case "October":

              echo "Its October";

              break;

          case "November":

              echo "Its November";

              break;

          case "December":

              echo "Its December";

              break;

          default:

              echo "Doesn't exist";

      }

      ?>

      Output:

      Its February
      

      Flowchart:

      What are the different types of selection statements in php?

    Ternary Operators

    In addition to all this conditional statements, PHP provides a shorthand way of writing if…else, called Ternary Operators. The statement uses a question mark (?) and a colon (:) and takes three operands: a condition to check, a result for TRUE and a result for FALSE.
    Syntax:

    (condition) ? if TRUE execute this : otherwise execute this;

    Example:

    <?php

    $x = -12;

    if ($x > 0) {

        echo "The number is positive \n";

    }

    else {

        echo "The number is negative \n";

    }

    echo ($x > 0) ? 'The number is positive'

                    'The number is negative';

    ?>

    Output:

    The number is negative
    The number is negative
    

    This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    What are the different types of statements that are present in PHP?

    In PHP we have the following conditional statements: if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more than two conditions.

    What are the selection statements?

    In java, the selection statements are also known as decision making statements or branching statements or conditional control statements. The selection statements are used to select a part of the program to be executed based on a condition.

    Which are selection statements explain with example?

    – Selection statements: if and switch – Iteration statements: while, do, and for – Jump statements: break, continue, and goto. (return also belongs in this category.) Several of C's statements must test the value of an expression to see if it is “true” or “false ” expression to see if it is true or false.

    What are the decision statements used by PHP?

    PHP - Decision Making.
    if...else statement − use this statement if you want to execute a set of code when a condition is true and another if the condition is not true..
    elseif statement − is used with the if...else statement to execute a set of code if one of the several condition is true..