PHP if…else

Posted On: March 4, 2016

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”);

Also Read :   PHP Switch

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”;
}
?>

X
Subscribe to our newsletter
Subscribe to our newsletter today to receive updates on the latest news and offers.
100% Privacy. No spam guaranteed
webe tutorial
Menu