PHP IF..Else..Elseif statement

PHP conditional statements are used for check or evaluate the different actions based on different conditions.

The result of these conditional are either TRUE or FALSE.

If the result is TRUE it means the condition is fulfilled for given action and the action will performed during execution of program.

If the result is FALSE it means the condition is not fulfilled for perform given action.

The actions are based on the logical or comparative conditions.

There are various form of conditional statements. Below we listed all the statements.

1. IF statement

1. IF…ELSE… statement

3. IF…ELSEIF… statement

4. Nested IF statement

IF Statement

In PHP, If statement is used for perform the action depend on some condition.

This is basic conditional statement.

If the condition is true then the block of code will execute otherwise it will not perform any action during run time.

This is the simplest and easiest way to represent conditional statement into PHP.

The block of code only execute only the given IF condition is true otherwise it will give blank result.

SYNTAX

if (condition)
{
    action or block of code
}

EXAMPLE :

<!DOCTYPE html>
<html>
<body>
<?php
$a = 10;
if ($a < 15) 
{
    echo "Welcome to demonuts..!";
}
?>
 
</body>
</html>

OUTPUT :

Welcome to demonuts..! 

In the above example we use if statement.

If the given condition ($a <15 ) is true then only the code inside if is executed.

IF…ELSE… statement

If-else statement contains two conditions.

If the given condition is true it will execute the block of code inside IF or the given condition is false it will run the block of code inside ELSE.

This statement is having code for both possibilities either true or false.

SYNTAX

if(condition)
{
   block of code
}
else
{
    block of code
}

EXAMPLE

<!DOCTYPE html>
<html>
<body>
<?php
$a = 10;
if ($a < 15) 
{
    echo "Number is less then 15";
}
else
{
	echo "Number is either greater than 15 or equal to 15";
}
?>
 
</body>
</html>

OUTPUT

Number is less then 15 

IF…ELSEIF…ELSE statement

This statement is combination of if-else statement.

These statement have multiple conditions for perform different actions.

It combine more than one if-else conditions into one program.

SYNTAX

if(condition)
{
   block of code
}
elseif
{
   block of code
}
else
{
   block of code
}

EXAMPLE

<!DOCTYPE html>
<html>
<body>
<?php
$a = 10;
if ($a < 15) 
{
    echo "Number is less then 15";
}
elseif ($a > 15)
{
	echo "Number is greater than 15";
}
else 
{
	echo "Number is equal to 15";
}
?>
 
</body>
</html>

OUTPUT

Number is less then 15 

Nested IF statement

It contain only IF statement inside IF block.

This statement checks more than one if conditions for perform action.

If the first condition is true then it will execute another IF condition which is given within first IF statement.

SYNTAX

if(condition)
{
   if(condition)
  {
     block of code
  }
}

EXAMPLE

<!DOCTYPE html>
<html>
<body>
<?php
$a = 60;
$b = 75;
if ($a > 55) 
{
	if($b > 70)
    {
    	echo "all the condition is true";
    }
}
?>
 
</body>
</html>

OUTPUT

all the condition is true 

Program to check number is positive or negative and even or odd using if…else…

<!DOCTYPE html>
<html>
<body>
<?php
$num = "9";
if($num < 0)
{
	if($num%2 == 0)
    {
		echo $num." is Even and Negative number";    
    }
    else
    {
    	echo $num." is Odd and Negative number";
    }
}
else
{
	if($num%2 == 0)
    {
		echo $num." is Even and Positive number";    
    }
    else
    {
    	echo $num." is Odd and Positive number";
    }
}
?>
 
</body>
</html>

OUTPUT

9 is Odd and Positive number 

Given example we use nested if condition to check number is even or odd and also check positive or negative.