PHP if - else
Conditional statement is used where we have to take different decisions on different conditions.The PHP if-else conditional statement is very similar to the C.The PHP if-else conditional statement can be utilized in three ways.
- Using Only if using only if statement performs a single task only if the condition is true and does nothing if the condition goes false.
- Using if-else using if-else, if the condition is true the specified task is performed and if it is false the code in the else block will be executed.
- Using if-elseif-elseThis is the complete form of PHP if-else conditional statement where different conditions are tested and the action is performed accordingly.
if ($percentage < 40)
{
echo "Sorry you didn't pass the course!!!";
}
{
echo "Sorry you didn't pass the course!!!";
}
In the above shown example only a single if statement is used.If the condition is true i.e $percentage variable is less than 40 than the message is echoed else nothing happens.
if ($percentage < 40)
{
echo "Sorry you didn't pass the course!!!";
}
else
{
echo "Congrats!!! You have passed the course.";
}
{
echo "Sorry you didn't pass the course!!!";
}
else
{
echo "Congrats!!! You have passed the course.";
}
Now the else block is also added in the code, in this case if the condition goes false the execution will jump into the else block
if ($percentage < 40)
{
echo "Sorry you didn't pass the course!!!";
}
elseif ($percentage < 60)
{
echo "Congrats!!! You have passed in C grade.";
}
elseif ($percentage <80)
{
echo "Congrats !!! You have passed in B grade.";
}
else
{
echo "Great!!! You have acheived the A grade.";
}
{
echo "Sorry you didn't pass the course!!!";
}
elseif ($percentage < 60)
{
echo "Congrats!!! You have passed in C grade.";
}
elseif ($percentage <80)
{
echo "Congrats !!! You have passed in B grade.";
}
else
{
echo "Great!!! You have acheived the A grade.";
}
Multiple condition check is performed in this code.If the first condition is false than the second condition is checked and so on.Finally if no condition is true,the code in the else block is executed.
0 comments:
Subscribe to:
Post Comments (Atom)
Post a Comment