PHP - Else IF

We already discussed on simple if and if..else in this topic we discuss on elseif and nested if statement it also called inner if condition the following syntax and example of both type condition in php.

Else If

Use the if..elseif..else statement to select one of sevral blocks of code to be executed.the following syntax and example of elseif condition.

Syntax


<?php
	if(condition)
	{
		//Code to be Executed if Condition is True
	}
	elseif
	{
		//Code to be Executed elseif Condition is True
	}
	else
	{
		//Code to be Executed if Condition is False
	}
?>							
							

Example


<?php
	$a=20;
	if($a<20)
	{
		echo "The Value of a is Less Than 20";
	}
	elseif($a>20)
	{
		echo "The Value of a is Greater Than 20";
	}
	else
	{
		echo "The Value of a is Equals to 20";
	}
?>							
							
							

Nested If

Use the Nested if..else statement to select one of several blocks which also contains the selection of blocks nested it code to be executed.the following syntax and example of nested if.

Syntax


<?php
	if(condition)
	{
		if(condition)
		{
			//Code to be Executed elseif Condition is True
		}
		else
		{
			//Code to be Executed elseif Condition is False
		}
	}
	else
	{
		if(condition)
		{
			//Code to be Executed elseif Condition is True
		}
		else
		{
			//Code to be Executed elseif Condition is False
		}
	}
	}
?>						
							

Example


<?php
	$a=5;
	$b=10;
	$c=20;
	if($a>$b)
	{
		if($a>$c)
		{
			echo "Largest Value is $a";
		}
		else
		{
			echo "Largest Value is $c";
		}
	}
	else
	{
		if($a>$b)
		{
			echo "Largest Value is $c";
		}
		else
		{
			echo "Largest Value $b";
		}
	}
?>								
							
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!