PHP Conditional Statement
Conditional statement execute depending on each other condition. There are four Conditional statement.
1) If.....statement
2) If...else..statement
3) If...esleif....else...statement.
4) Switch...statemnent.
If.....statement
If the condition(within if bracket) is true at that time result will execute.
Syntex of If.....statement
<?php
if(condition)
{
execute if condition is true;
}
?>
Example
<php
$st_info='Hamid';
if($st_info=='Hamid')
{
echo"Due of Hamid is 10,000";
}
?>
According to above example we are finding that if $st_info equal to Hamid than output will be "Due of Hamid is 10,000i". But now another question is arised that if $st_info !="Hamid"(not equal to) at that time what will happen?. In this case we have to use If...else.. statement and example of If....Else statement is given below.
Syntax of If.... else..statement
<?php
if(condition)
{
execute if condition is true;
}
else
Otherwise it will execute alternative result
?>
Example
<?php
$st_info='Hamid';
if($st_info=='Hamid')
{
echo"Due of Hamid is 10,000";
}
else {
echo"There is no student of this name";
}
?>
If there is more condition to apply at that time we have to use If....elseif.....else statement.Like grading system of result.
Syntax of If... elseif... else..statement
<?php
if(condition)
{
execute if condition is trure;
}
elseif(condition)
{
execute elseif condition is true
}
else
otherwise it will execute
?>
Example
<php
$number=80;
if($number>=80)
{
echo"A+";
}
elseif ($number>=70 )
{
echo"A";
}
elseif ($number>=60 )
{
echo"A-";
}
elseif ($number>=50 )
{
echo"B";
}
elseif ($number>=40 )
{
echo"C";
}
else
echo"F";
?>