Open In App

PHP | Decision Making

Last Updated : 05 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Output
The 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";
}
?>

Output
The 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";
}
?>

Output
Happy 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!";
}
?>

Output
Start of the 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.";
?>

Output
You are an adult.

Next Article
Practice Tags :

Similar Reads