# If Else

0 zelmacanning506 December 7, 2020

If we have to execute a block of statements when a particular condition is met or not met. This is known as Decision making. On that condition, we will use if else control statement. For decision making, We have 4 types of control statements in PHP programming.

• if statement
• if….else statement
• else…..if statement
• nested if….else statement

## 1. IF Statement:

If the statement is a powerful decision-making statement and is used to control the flow of execution of statements. It basically a two-way decision statement and is used together with an expression, i.e. test condition. The if statement evaluates the expression first and then, if the value the expression is true, it executes the statement within the block. Otherwise, it skips the statements within its block and continues from the first statement outside the if block. It takes the form,

``````if(condition)
{
statement-block;
}
statement-x;``````

### Flow Diagram

Let’s take an example to make clear concepts on IF-ELSE.

``````<?php

\$num = 20;
if(\$num % 2 == 0){
echo "Number is Even";
}

?>``````

The output of the above program is:

``Number is Even``

## 2. IF…ELSE Statement

Theif…else statement is an extension of the simpleifstatement. It uses when there are two possible actions – one when a condition is true, and the other when it is false. The general form is

``````if(condition)
{
true_block statement;
}
else
{
false_block statement;
}
statement-x;``````

### Flowchart

Let’s look upon one example to make clear concepts on if..else condition.

``````<?php

\$num = 20;
if(\$num % 2 == 0)
{
echo "Number is Even";
}
else
{
echo "Number is Odd";
}

?>``````

The output of the above program is:

``Number is Even``

## 3. ELSE…IF Statement

The else if the statement is used when there are more than two possible actions depending upon the outcome of the test. When an action is taken, no others can be executed or taken. In such a situation, the more than one if…else statement is used. This structure takes the form,

``````if(condition-1)
{
statement-1;
}
else if(condition-2)
{
statement-2;
}
else if(condition-3)
{
statement-3;
}
else
{
default-statement;
}``````

Let’s look at one example to make clear concepts on else…if condition.

``````<?php

\$num = 20;
if(\$num % 2 == 0)
{
echo "Number is Even";
}
else
if(\$num % 3 == 0)
{
echo "Number is Odd";
}
else
{
echo "Invalid Number";
}

?>``````

The output of the above program is:

``Number is Even``

## 4. Nested IF…ELSE Statement

Nested IF…ELSE statement means there is anIF condition inside the if condition. The syntax looks like this.

``````if(condition)
{
if(condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}``````

Let’s look at examples to make clear concepts on nested if…else condition.

``````<?php

\$num = 20;
if(\$num % 2 == 0)
{
if(\$num > 5 == 0)
{
echo "Number is greater than 5 and Even\n";
}else{
echo "Number is lessa than 5 andEven\n";
}
}
else
if(\$num % 3 == 0)
{
echo "Number is Odd";
}
else
{
echo "Invalid Number";
}

?>``````

The output of the above program is:

``Number is greater than 5 and Even``