Skip to content
SW Study Warehouse
Home › Blog
Blog

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

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

Understanding if else statement in php gives you clear, practical steps and best practices you can apply right away. Below is the complete guide, structured examples, and key takeaways.

Complete Guide: If Else Statement In Php

The if statement is quite basic because in If statement output will display when condition must be true if a condition is false it displays nothing(blank). But if-else statements allows you to display output in both the condition(if a condition is a true display some message otherwise display another message). In English, this statement would read, “if X happens, do Y; otherwise do Z”.

Syntax

if (condition) {
    code to be executed if condition is true;
} else {
    code to be executed if condition is false;
}
The example below will output “Have a good day!” if the current time is less than 20, and “Have a good night!” otherwise
<?php
$t = date("H");

if ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}
?>
Example to find the number is Odd or Even
<?php  
$num=12;  
if($num%2==0){  
echo "$num is even number";  
}
else
{  
echo "$num is odd number";  
}  
?> 
Output: 12 is even number

Key Takeaways & Resources for If Else Statement In Php

For further details, consult Official PHP Manual or explore 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 *