If Else statement is the most important feature of any programming language, PHP is used to run any Code Of Block according to If Else conditions. This means when we have to run some other code when the condition is true, then we use If Else if the condition is false.
Types of If Else Conditionals
- if Statement
- if…else Statement
- if…elseif…else Statement
1. if Statement
The if statement is executed only when the condition is true. If the condition is false then nothing is executed.
Example:-
<?php
$value = 14;
if ($value < 20) {
echo "Have a nice day";
}
?>
Output:-
2. If-else statement
There are two types of conditions in the if….else statement. One, when the condition is true then the code should be executed and when the condition is false then the statements should be executed.
Example:-
<?php
$num = 70;
if ( $num > 7 ) {
echo "Code to be executed if condition is true";
} else {
echo "Code to be executed if condition is false";
}
?>
Outputs:-
3. if…elseif…else Statement
It is a combination of conditional statements if…else. The only difference is that the if…else statement has been used multiple times. And more than two conditions can be executed in this.
Example:-
<?php
$age = 19;
//If else ladder
if ($age>18){
echo "You are drink alcohol";
}
elseif($age>13){
echo "You are drink chai with water";
}
else{
echo "You can drink water only";
}
?>