Decision-making is an important part of programming, allowing the program to execute different actions based on conditions. In PHP, decision-making helps control the flow of a program by executing different blocks of code depending on certain conditions or expressions. PHP provides several constructs for decision-making, including if, else, elseif, and switch. These control structures can be used to make logical decisions in a program.
This article covers the decision-making structures in PHP and explains how to use them effectively. Let us now look at each one of these in detail:
1. if Statement
The if statement is the simplest form of decision making. It executes a block of code if the specified condition evaluates to true. If the condition evaluates to false, the block of code is skipped.
Syntax:
if (condition){
// if TRUE then execute this code
}
Example:
PHP <?php $x = 12; if ($x > 0) { echo "The number is positive"; } ?>
OutputThe number is positive
Flowchart

2. if...else Statement
The if-else statement is an extension of the if statement. It allows you to specify an alternative block of code that is executed when the condition is false. This is useful when there are two mutually exclusive conditions.
Syntax:
if (condition) {
// if TRUE then execute this code
}
else{
// if FALSE then execute this code
}
Example:
PHP <?php $x = -12; if ($x > 0) { echo "The number is positive"; } else{ echo "The number is negative"; } ?>
OutputThe number is negative
Flowchart

3. if...elseif...else Statement
In scenarios where you need to evaluate multiple conditions, you can use the if-elseif-else ladder. This construct allows you to check multiple conditions in sequence. The first condition that evaluates to true will execute its corresponding block of code, and all other conditions will be skipped.
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 <?php $x = "August"; if ($x == "January") { echo "Happy Republic Day"; } elseif ($x == "August") { echo "Happy Independence Day!!!"; } else{ echo "Nothing to show"; } ?>
OutputHappy Independence Day!!!
Flowchart

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.
- The break statement is used to stop the automatic control flow into the next cases and exit from the switch case.
- The default statement contains the code that would execute if none of the cases match.
Syntax
switch (variable) {
case value1:
// Code to be executed if the variable equals value1
break;
case value2:
// Code to be executed if the variable equals value2
break;
default:
// Code to be executed if no cases match
}
Example:
PHP <?php $day = "Monday"; switch ($day) { case "Monday": echo "Start of the week!"; break; case "Friday": echo "End of the week!"; break; case "Saturday": case "Sunday": echo "Weekend!"; break; default: echo "Mid-week!"; } ?>
Note:
- The switch statement compares the variable’s value strictly (both type and value).
- If no case matches, the default block is executed (if provided).
- The break statement is crucial; without it, the program will continue to execute subsequent case blocks (a behavior known as "fall-through").
Flowchart

5. Ternary Operators
PHP also provides a shorthand way to perform conditional checks using the ternary operator (?:). This operator allows you to write simple if-else conditions in a compact form.
Syntax
(condition) ? if TRUE execute this : otherwise execute this;
Example:
PHP <?php $age = 20; echo ($age >= 18) ? "You are an adult." : "You are a minor."; ?>
Similar Reads
PHP match Expression The PHP match expression is used for the identity check of a value. It is similar to the switch statement i.e. it matches the expression with its alternative values. The match expressions are available in PHP 8.0.0. The match expression compares the value using a strict comparison operator (===) wh
1 min read
PHP | is_double() Function The is_double() function is an inbuilt function in PHP which is used to find whether the given value is a double or not. Syntax: bool is_double( $variable_name ) Parameters: This function accepts one parameter as mentioned above and described below: $variable_name: This parameter holds the value whi
2 min read
PHP File Handling In PHP, File handling is the process of interacting with files on the server, such as reading files, writing to a file, creating new files, or deleting existing ones. File handling is essential for applications that require the storage and retrieval of data, such as logging systems, user-generated c
4 min read
How to handle exceptions in PHP ? Exceptions in PHP: The exception is the one that describes the error or unexpected behavior of the PHP script. The exception is thrown in many PHP tasks and classes. User-defined tasks and classes can also do differently. The exception is a good way to stop work when it comes to data that it can use
2 min read
PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi
8 min read