PHP Conditional Statements
Conditional Statements are those statements which will be executed only when the condition is satisfied.
1.If Statement
The IF statement is executed if the condition is true.
Syntax : if( condition) { //your code here }
Example:
<?php
$a = 10;
$b = 5;
if($a > $b)
{
echo "a is greater than b";
}
?>
2.If Else Statement
In the IF Else statement, the "IF" part is executed if the condition is true else the "Else" part will be executed.
Syntax :
if( condition)
{
//your code here
}
else
{
//your code here
}
Example :
<?php
$a = 10;
$b = 5;
if($a > $b)
{
echo "a is greater than b";
}
else
{
echo "b is greater than a";
}
?>
3.If Elseif Else Statement
In the "IF Elseif Else" statement, first the compiler check if the "IF" condition is true.If it is true, the respective code will be executed. But when the "IF" condition is false, the compiler will check if the "elseif" condition is true. If is true, the respective code will be executed. If both the "IF" and "Elseif" conditions are not satisfied, then the "Else" part will be executed.
Syntax :
if( condition)
{
//will be executed when the above if condition is true.
}
elseif( condition )
{
//will be executed when the if condition is false and the elseif condition is true.
}
else
{
//will be executed when none of the above conditions are satisfied.
}
Example :
<?php
$a = 10;
$b = 5;
if($a > $b)
{
echo "a is greater than b";
}
elseif( $b > $a)
{
echo "b is greater than a";
}
else
{
echo "a is equal to b";
}
?>