elseif ladder Statement in PHP
Notes:
PHP elseif ladder statement:
- helps us to check multiple conditional expressions
syntax:
if(conditional expression 1)
{
statements; // true part of the if (conditional expression 1)
}
elseif(conditional expression 2)
{
statements; // true part of the elseif (conditional expression 2)
}
…
elseif(conditional expression n)
{
statements; // true part of the elseif (conditional expression n)
}
else
{
statements; // false part of the elseif (conditional expression n)
// if no conditional expression evaluates to true, then else block is executed
}
OR
if(conditional expression 1):
statements; // true part of the if (conditional expression 1)
elseif(conditional expression 2):
statements; // true part of the elseif (conditional expression 2)
…
elseif(conditional expression n):
statements; // true part of the elseif (conditional expression n)
else:
statements; // false part of the elseif (conditional expression n)
// if no conditional expression evaluates to true, then else block is executed
endif;
Example Code:
$day = 8;
if($day==1)
{
echo "Sunday";
}
elseif($day==2)
{
echo "Monday";
}
elseif($day==3)
{
echo "Tuesday";
}
elseif($day==4)
{
echo "Wednesday";
}
elseif($day==5)
{
echo "Thursday";
}
elseif($day==6)
{
echo "Friday";
}
elseif($day==7)
{
echo "Saturday";
}
else
{
echo "Invalid day";
}
OR
$day = 8;
if($day==1):
echo "Sunday";
elseif($day==2):
echo "Monday";
elseif($day==3):
echo "Tuesday";
elseif($day==4):
echo "Wednesday";
elseif($day==5):
echo "Thursday";
elseif($day==6):
echo "Friday";
elseif($day==7):
echo "Saturday";
else:
echo "Invalid day";
endif;
Interview Questions:
1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a