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.
Quick Navigation
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.


