Home Blog if-else Statement in PhP

if-else Statement in PhP

0
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

LEAVE A REPLY

Please enter your comment!
Please enter your name here