In PHP, if-else is used for conditional statements. there are 3 types to use if-else condition.
1) if condition:- “if” condition will be executed when “if” statement is true.
Syntax:-
<?php
if(conditional_expression)
{
}
?>
Example:- Suppose, You have $x variable and that has value 100 and now you create the condition.
<?php
$x=100;
if($x>90)
{
echo 'condition is passed';
}
?>
condition is passed
2) if else condition:- “if” condition will be executed when “if” statement is true otherwise false condition will be executed.
Syntax:-
<?php
if(conditional_expression)
{
}else{
}
?>
Example:- Suppose, You have $x variable and that has value 80 and now you create the condition.
<?php
$x=80;
if($x>90)
{
echo 'condition is passed';
}else{
echo 'condition is failed';
}
?>
condition is failed
3) if elseif condition:- “if” condition will be executed when “if” statement is true otherwise check else if condition.
Syntax:-
<?php
if(conditional_expression1)
{
}elseif(conditional_expression2)
{
}else{
}
?>
Example:- Suppose, You have $x variable and that has value 80 and now you create the condition.
<?php
$x=80;
if($x>90)
{
echo 'x value is greater than 90';
}elseif($x>70 && $x<90){
echo 'x value is between 70 to 90';
}else{
echo 'condition does not match';
}
?>
x value is between 70 to 90