Decision making in php w3schools


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:

$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:

$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!":

$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



PHP conditional statements allow you to make a decision, based upon the result of a condition. These statements are called as Decision Making Statements or Conditional Statements.

The flowchart of Decision-making technique in PHP can be expressed as:

Decision making in php w3schools

PHP has such decision-making capabilities within its program by the use of following decision-making statements

Conditional Statements in PHP

  • if else statement
  • elseif statement
  • switch statement


Decision making in php w3schools
report this ad

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:

      $x = 12;

      if ($x > 0) {

          echo "The number is positive";

      }

      ?>

      Output:

      The number is positive
      

      Flowchart:

      Decision making in php w3schools

    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:

      $x = -12;

      if ($x > 0) {

          echo "The number is positive";

      }

      else{

          echo "The number is negative";

      }

      ?>

      Output:

      The number is negative
      

      Flowchart:

      Decision making in php w3schools

    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:

      $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:

      Decision making in php w3schools

    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:

      $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:

      Decision making in php w3schools

    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:

    $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 is decision making in PHP?

    PHP conditional statements allow you to make a decision, based upon the result of a condition. These statements are called as Decision Making Statements or Conditional Statements.

    What are the 4 PHP conditional statements?

    The conditional statements in PHP are if, else, elseif, and switch.

    What are control statements in PHP?

    Control statements are a basic component of all modern-day programming languages like PHP, Java, Python, Go, Ruby, NodeJS. These control statements allow software developers and architects to decide how the software or program they develop will behave under different conditions.

    What are the different types of selection statements in PHP?

    PHP 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.