Introduction

PHP 8 contains three types of major conditional statements, If…Else…ElseIf conditional statements. In this article, you will learn the PHP conditional statements starting from the basics to their advanced usage.

What are PHP Conditional Statements?

PHP Conditional Statements specify a condition to run the code block of the conditional. When the conditional statement becomes true, the instruction pointer moves to the first statement inside the conditional block. If the condition does not meet, the instruction pointer will skip the whole block of code and moves to the next statement outside the block.

Types of PHP conditional statements

In PHP 8, we have the following types of conditional statements. Their basic principle remains the same, however, the choice among these types depends on the scenario for the developer.

  1. If
  2. Else
  3. Else If
  4. Switch

The switch statement is different from the first three, therefore, we will throw light on the switch statement after explaining the first three types in detail.

IF statement in PHP

IF statement in PHP receives an argument, which is either true or false. The code block of the statement executes when the argument is true. On the other hand, if the arguments return false, the code block lines will not execute.

General Syntax of IF Statement

if (condition) {
  code block that will execute when the condition becomes true
}

Example

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

if ($a < "10") {
  echo "I am inside the if block!";
}
?>

Explanation of example

  • The variable a is initialized with the date. The only current hour is taken out of the date.
  • If the statement is used to check if the current hour is less than 20.
  • If the above statement returns true. The message is printed.

Note: If you are confused about getting the Hour from the date, you can go through the Time and Date article.

The if statement works pretty fine for the single decisions, what if there is an alternate statement that must execute if the condition becomes false. In this case, we have IF ELSE conditional in PHP.

IF ELSE Statement in PHP

PHP allows you to handle both the true and false cases of the conditional statement using IF-ELSE statement. If the condition specified in the IF statement does not return true, it will execute the code block of the else statement. Look at the following example to have a clear idea.

General Syntax of IF ELSE statement in PHP

if (condition) {
  code block;
} else {
  code block;
}
<?php
$a = date("H");

if ($a < "10") {
  echo "Inside if!";
} else {
  echo "Inside else!";
}
?>

Explanation of the Example

  • In the above example, the variable $a contains the current date. The “H” specified in the date parameter indicated that we only want the current hour of the day.
  • Check if the current hour is less than 20.
  • If the current hour is not less than 0 or greater than it, execute the else block

If you have multiple conditions. use the following type of conditional statement.

The general syntax of IF ElseIf condition

if (condition) {
  code block if condition becomes true;
} elseif (condition) {
  code block if else if becomes true;
} else {
  code block if none of the above conditions becomes true;
}
<?php
$a = date("H");

if ($t < "20") {
  echo "Inside first if!";
} elseif ($t < "20") {
  echo "Inside else if!";
} else {
  echo "Inside last else!";
}
?>

Explanation of the example

  • In the above example, the variable $a contains the current date or time.
  • The first condition is, If the Hour is less than 10, print “Have a good morning!”.
  • The second condition is, If the current time is not less than 10, don’t worry, Elseif statement is there to put a check for another condition that is $a should be less than 20.
  • If both of the statements do not fall into any condition, last else statement will execute.

Switch statement in PHP

The switch statement executes different code blocks based on the different conditions. The switch statement executes only one statement that triggers the switch.

General Syntax of Switch Statement in PHP

switch (cond) {
  case label_1:
    code block if cond=label_1;
    break;
  case label_2:
    code block if cond=label_2;
    break;
  case label_3:
    code block if cond=label_3;
    break;
    ...
  default:
    code block if cond is different from all labels;
}

In the above syntax, the switch checks the condition cond in its all cases. It executes the block of code associated with that case when its condition is satisfied in the label. The break statement stops checking the next case automatically.

What is the default in the switch statement?

Default defines that if no case satisfies the condition in the switch statement, then the code block under the default keyword will execute.

Example of the switch statement in PHP

<?php
$color = "green";

switch ($color) {
  case "white":
    echo "Favorite color is white!";
    break;
  case "black":
    echo "Favorite color is black!";
    break;
  case "green":
    echo "Favorite color is green!";
    break;
  default:
    echo "Favorite color does not belongs to white, black, or green!";
}
?>

For official reference to PHP 8 conditionals, click here.