Home >>PHP Tutorial >PHP if elseif else Statement

PHP if elseif else Statement

Nested if else in PHP

The if-else-if-else statement lets you chain together multiple if-else statements, thus allowing the programmer to define actions for more than just two possible outcomes.

Check given character is vowel or consonant.

<?php	
$char=$_POST['ch'];
if($char=="a")
{
echo $char." is vowel";	
}
else if($char=="e")
{
echo $char." is vowel";
} 
else if($char=="i")
{	   
echo $char." is vowel";	
} 	
else if($char=="o")	
{	   
echo $char." is vowel";	
}  	
else if($char=="u")	
{	   
echo $char." is vowel";	
} 	
else	
{		
echo $char. "is consonent";	
}
?>	
<body>
<form method="post">
Enter Your number<input type="text" name="ch"/><hr/>
<input type="submit"/>
</form>
</body>
Output
d is consonant
Enter your number

First we create a textbox and a button using HTML script.
using $_POST[ ] collect value that is entered by user and store in a variable($char).
Now check the condition($char=="a"),($char=="e"),($char=="i"),($char=="0") and so on.
Here we use nested if else because we need to select one of several blocks to be executed.
if the character enter by user are (a,e,i,o,u) it print, character is vowel else character is consonant.
Here user has entered "d" so the output will display like : d is consonant

Print related day name according to inputted number (1 - 7).

<?php
	
$day=$_POST['day'];
	
if($day==1)
	 
{
	   
echo "Monday";	
	 
}
	
else if($day==2)
	
{
	   
echo "tuesday";
	
} 
	
else if($day==3)
	
{
	   
echo "wednesday";
	
}
	
else if($day==4)
	
{
	   
echo "Thursday";
	
}
	
else if($day==5)
	
{
	   
echo "friday";
	
}
	
else if($day==6)
	
{
	   
echo "Saturday";
	
}
	
else if($day==7)
	
{
	   
echo "Sunday";
	
}
	
else
	
{
		
echo "Wrong choice";
	
}

?>
	
<body>
	
<form method="post">
	   
Enter Your number<input type="text" name="day"/><hr/>
	    
<input type="submit"/>
	
</form>

</body>

Output
Friday
Enter your number

First we create a textbox and a submit button using HTML script.
using $_POST[ ] collect value that is entered by user and store in a variable($day).
Now check the condition($day==1),($day==2),($day==3) and so on.
Here we use nested if else because we need to select one of several blocks to be executed.
if the number entered by user are (1 - 7) it print related statement, else wrong choice.
Here user has entered the value "5" so the output will display like : Friday

Popular Tutorials