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

Latest Article

Blog

PHP Magic Methods

__get()/__set() – These are magic getters and setters for getting and putting values for class properties created dynamically. __isset() – This magic method will be invoked automatically while checking whether a required overloaded property is Read more…

Blog

WordPress Upgrade

The WordPress upgrade released time to time. We provide a WordPress upgrade, content management and maintenance services at affordable price. WordPress upgrade fixes bugs, runs cleaner, faster, and is more secure and stable. We will Read more…

Blog

Magento 2 Upload image in product

If you are trying to upload image on respective product by script then below code will help you to solve this issue. by this code you can set product thumbnail, small and large image. $objectManager Read more…