If…Else are used to execute dissimilar procedures based on different conditions.
if Condition:-
The if condition executes some code if one condition is true.
Syntax:-
if (condition) {
execution code if condition is true
;
}
Example:-
<?php
$dt = date(“H”);

if ($dt < “2”) {
echo “the current time is 2 o clock”;
}
?>
if…else Condition:-
The if….else condition executes some code if one condition is true and another code if condition is false.
Syntax:-
if (condition) {
execution code if condition is true;
} else {
 execution code if condition is false;
}
Example:-
<?php
$dt = date(“H”);

if ($dt < “2”) {
echo “the current time is 2 o clock”;
} else {
echo “the current time is not 2 o clock”;
}
?>
if…elseif….else Condition:-
The if….elseif…else condition executes different codes for more than two conditions.
Syntax:-
if (condition) {
execution code if this condition is true;
} elseif (condition) {
 execution code if this condition is true;
} else {
execution code if all conditions are false;
}
Example:-
<?php
$dt = date(“H”);

if ($dt < “1”) {
echo “the current time is not 2 o clock”;
} elseif ($dt < “2”) {
echo “the current time is 2 o clock”;
} else {
echo “the current time is not 2 o clock”;
}
?>

Categories: PHP

0 thoughts on “PHP if…else”

Leave a Reply