PHP if…else…elseif Statements
The PHP if-else statements are used to make decisions based on distinct conditions.
PHP keeps different types of decision-making conditions:
- if statement
- if…else statement
- if…else if….else statement
- switch statement
PHP – The if Statement
The body of the if statement performs some piece of code only if a specified condition is true.
Syntax
if(condition){
// Code to be executed here
}
Example
$x=15;
$y=15;
if($a==$b)
{
echo"successful";
}
The if…else Statement
The if….else statement is used where the if the condition is not true then the else statement is executed.
Syntax
if(condition){
// Code to be executed if condition is true
} else{
// Code to be executed if condition is false
}
Example
$x=15;
$y=15;
if($a==$b)
{
echo"successfuly executed";
}else
{
echo"loss";
}
PHP – The if…elseif….else Statement
The if…elseif…else is a special statement to execute when multiple conditions are true.
if(condition1){
// Code to be executed if condition1 is true
} elseif(condition2){
// Code to be executed if the condition1 is false and condition2 is true
} else{
// Code to be executed if both condition1 and condition2 are false
}
Example
$x=15;
$y=15;
$z=15;
if($x > $y and $x > $z)
{
echo"x is biggest.";
}
elseif($y > $x and $y > $z)
{
echo"y is biggest";
}
elseif($z > $x and $z > $y)
{
echo" c is biggest";
}
else
{
echo"x=y=z";
}