PHP If, Else, ElseIf
Operator if - is one of the most important operators in PHP.
The syntax of the conditional operator:
if (condition) operator;
Condition - a logical expression that can be true (TRUE) or false (FALSE).
The operator - is executed when the condition is true and is not executed when the condition is false!
<?php
if(2 > 0)
echo ( "Hello Web !!!");
?>
In this example operator echo ( "Hello W3docs !!!"); was executed, because 2 > 0 is true!
If we want to use several operators, we need to enclose them in braces {and }:
<?php
if(2 > 0)
{
echo ( "Hello ");
echo ( "Web !!! ");
echo ( "!!!");
}
?>
Operator if .. else - is used when we need to execute code if a condition is true and another code if the condition is false.
The syntax :
if (condition) operator1; else operator2;
If the condition is true, then executed operator1, if false then operator2.
If operator1 or operator2 should consist of several teams, then they are enclosed in braces.
<?php
if(2 > 0)
{
echo ( "true");
}
else
{
echo ( "false");
}
?>
In this example, the echo ( "true"); operator will be executed, because condition (2 > 0) is true!
Operator if .. elseif .. else - is used, when we need to check another condition when the first condition is false.
The syntax :
if (condition) operator1; elseif (contition2) operator2; else operator3;
If condition1 is true, executed operator1, if false, then checked condition2, if condition2 is true, executed operator2, if false then executed operator3.
<?php
if(2 < 0)
{
echo ( "2 < 0 is true");
}
elseif(2 > 0)
{
echo ( "2 > 0 is true");
}
else
{
echo ( "conditions are false");
}
?>
In this example, operator echo ( "2 > 0 is true"); will be executed, because condition (2 > 0) in elseif is true !
0 Comments
CAN FEEDBACK
Emoji