Conditional statements in PHP

Strings in PHP
Variables and Constants

In PHP 8, there are three sorts of main conditional statements: If…Else… ElseIf statements are conditional statements. In this article, you will learn about PHP conditional statements, from the fundamentals to sophisticated applications.

What are Conditional Statements?

PHP conditional statements define a condition that must be met in order for the conditional code block to be executed. When the conditional statement is satisfied, the instruction pointer is moved to the first statement within the conditional block. If the condition is not satisfied, the instruction pointer skips the whole block of code and goes to the next statement outside the block.

Types of PHP conditional statements

Conditional statements in PHP 8 are classified as follows. Their core idea stays the same; nevertheless, the developer’s decision between these sorts is determined by the circumstance.

  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

In PHP, the IF statement is given an argument that is either true or false. When the argument is true, the statement’s code block is executed. If the parameters return false, the code block lines will not be executed.

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

<?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

Using the IF-ELSE statement, PHP allows you to handle both the true and false situations of a conditional statement. If the condition stated in the IF statement does not return true, the code block of the else statement is executed. Consider the following example to have a better understanding.

if (condition) {
  code block;
} else {
  code block;
}
$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.

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;
}
$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 runs various code blocks depending on the criteria. The switch statement only executes the statement that causes the switch to be activated.

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;
}

The switch in the preceding syntax examines the condition condition in all circumstances. When the condition in the label is met, it executes the block of code associated with that case. The break statement instantly stops checking the following case.

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

$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!";
}

Q&A

Q: What is a conditional statement?
A conditional statement is a programming technique that allows you to run code only if a given condition is met.

Q: In PHP, how do you write an if statement?
A: The if keyword is used to form an if statement, which is then followed by the condition in parentheses and a set of curly braces holding the code to be executed if the condition is true. For instance, if (condition) / code to be performed;

Q: How do you write an if-else statement?
A: The if keyword is used to build an if-else statement, which is then followed by the condition in parentheses and a set of curly braces holding the code to be executed if the condition is true. Then, if the condition is false, an otherwise keyword is followed by a set of curly brackets containing the code to be executed. if (condition) / code to be performed if true; else / code to be executed if false;

Q: How do you write an if-elseif-else statement?
A: The if keyword is used to form an if-else statement, which is followed by the first condition in parentheses and a set of curly braces containing the code to be run if the condition is true. Then comes an elseif keyword, followed by the second condition in parenthesis and a pair of curly braces holding the code that will be performed if the second condition is true. Finally, there is an else keyword followed by a pair of curly brackets holding the code that will be run if both conditions are false. Example:

elseif (condition2) {  // code to be executed if condition1 is false and condition2 is true; } 
else {  // code to be executed if both conditions are false; }

Q: How do you write a switch statement?
A: A switch statement is produced by employing the keyword ‘switch,’ followed by the value to be evaluated in parentheses and a series of curly brackets ” containing the various cases. Each case is defined by the keyword ‘case,’ followed by the value to be matched and a colon ‘:,’ and finally the code to be run if the case is matched. The ‘default’ keyword, followed by a colon ‘:’ and the code to be run if none of the cases are matched, can be used to introduce a default case.

Q: What is the difference between an if-else statement and a switch statement?
A: The primary distinction between if-else and switch statements is that if-else statements assess numerous circumstances and execute code appropriately, whereas switch statements compare a single value against multiple scenarios and execute code accordingly. If-else statements can deal with any kind of condition or expression, whereas switch statements can only deal with integral types and strings.

Exercises:

  1. How do you create an if statement in PHP?
  2. How do you create an if-else statement in PHP?
  3. How do you create an if-elseif-else statement in PHP?
  4. How do you create a switch statement in PHP?
  5. What is the difference between if-else and switch statements?
  6. How do you use ternary operator to create a short if-else statement in PHP?

Answers:

  1. if (condition) { // code to be executed; }
  2. if (condition) { // code to be executed if true; } else { // code to be executed if false; }
  3. if (condition1) { // code to be executed if condition1 is true; } elseif (condition2) { // code to be executed if condition1 is false and condition2 is true; } else { // code to be executed if both conditions are false; }
  4. switch (value) { case value1: // code to be executed if value1 is matched; break; case value2: // code to be executed if value2 is matched; break; default: // code to be executed if none of the cases are matched; }
  5. If-else statements are used to assess many conditions and then execute code, whereas switch statements are used to test a single value against numerous scenarios and then execute code. If-else statements can deal with any kind of condition or expression, whereas switch statements can only deal with integral types and strings.
  6. $result = (condition) ? value_if_true : value_if_false;
Strings in PHP
Variables and Constants
en English
X
Scroll to Top