Skip to content
SW Study Warehouse
Home › Blog
Blog

if-elseif-else Statement in PHP (Complete 2026 Syntax & Examples)

By Pardeep Patel · November 25, 2026 · 2 min read 👁 269 Views
PhP
Disclosure: Some links in this guide are affiliate links. We may earn a commission at no extra cost to you.

Understanding if elseif else statement in php is an essential building block for beginners and students writing clean, reliable code. Below you will find the step-by-step syntax, working example, and key rules to follow.

Complete Guide: If Elseif Else Statement In Php

The if-elseif-else statement lets you chain together multiple if-else statements, thus allowing the programmer to define actions for more than just two possible outcomes.

Syntax

if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if this condition is true;
} else {
    code to be executed if all conditions are false;
} 
The example below will 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!”
<?php
$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!";
}
?>
The following example will output “Have a nice weekend!” if the current day is Friday, and “Have a nice Sunday!” if the current day is Sunday, otherwise it will output “Have a nice day!”
<?php
$d = date("D");
if($d == "Fri")
{
echo "Have a nice weekend!";
} 
elseif($d == "Sun")
{
echo "Have a nice Sunday!";
} 
else
{
echo "Have a nice day!";
}
?>

Ternary Operator

The ternary operator provides a shorthand way of writing the if…else statements. The ternary operator is represented by the question mark (?) symbol and it takes three operands: a condition to check, a result for ture, and a result for false. To understand how this operator works, consider the following examples:
<?php

    if($age < 18){

        echo 'Child'; // Display Child if age is less than 18

    } else{

        echo 'Adult'; // Display Adult if age is greater than or equal to 18

    }

    ?>
Using the ternary operator the same code could be written in a more compact way
<?php echo ($age < 18) ? 'Child' : 'Adult'; ?>
The ternary operator in the example above selects the value on the left of the colon (i.e. ‘Child’) if the condition evaluates to true (i.e. if $age is less than 18), and selects the value on the right of the colon (i.e. ‘Adult’) if the condition evaluates to false.

Key Takeaways & Official Reference for If Elseif Else Statement In Php

When practicing if elseif else statement in php, test edge cases carefully and keep your formatting consistent. For deeper reference, review the Official PHP Manual or continue with our Basic Syntax of PHP.

Found this guide helpful? Support Team StudyWarehouse! Buy Us a Coffee

Pardeep Patel

Hi!, I am Pardeep Patel, an Indian passport holder, Traveler, Blogger, Story Writer. I completed my M-Tech (Computer Science) in 2016. I love to travel, eat different foods from various cuisines, experience different cultures, make new friends and meet other.

Leave a Reply

Your email address will not be published. Required fields are marked *