PHP IF ELSE


php tutorial

IF ELSE

php tutorial

In programming if we want to put conditional statements according to status of expression's result then we can apply If Else. Generally If used to control true part of specified condition and Else just opposite of it. we can't put condition with Else.

Syntax

		If ( condition )
		{
			statements;
		}
		else
		{
			statements;
		}

Example

In below mention code of php finding odd or even no using if - else.

		<?php
		
		$num=2;
		if($num%2==0)		//testing number weather it is even or odd
		{
			echo "Even";
		}
		else
		{
			echo "Odd";
		}
		
		?>

						

Nested If Else

It is used to put alternate condition on nth level for if. It works like ladder and also called Ladder If Else.

Syntax

		If ( condition )
		{
			statements;
		}
		else if (alternate condition)
		{
			statements;
		}
		else
		{
			statements;
		}
		
		

Example

In below mention code of php, finding weekday of a week using if - else (Nested).

		<?php
		$day=1;
	
		if($day==0)		
		{
			echo "Sunday";
		}
		else 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";
		}
		?>