What are If, Nested if, if else and, Nested if else Statement in PHP?

If Statement and Nested if Statement

If statement is used to execute when a statement or a block of statement only if the condition is fulfilled or true.

Syntax

For One Statement:-

if(condition/expression)
statement;

For more than one Statement:-

if(condition/expression)
{
Blocks of statement;
}

OR

if(condition/statement):
Blocks of statements;
endif;

Sample Program

Output

Nested if Statement

We can make nesting statement by nesting the if statement. It means when we insert a second if statement inside an if statement, we call it nested if statement or nesting the if statement.

Sample Program

Output

To know more about If Statement and Nested if Statement Click here – If Statement and Nested if Statement

if else and Nested if else Statement.

A statement executes code if if_condition is true and executes another code if if_condition is false.

Syntax

if(condition/expression)
{
Statement1;
}
else echo “Statement2”;

Example – when Condition is True then, if_condition executes. See Below

Sample Program

Output

when Condition is False then, else_condition executes. See Below

Sample Program

Output

Nested if else Statement

we insert a second if_else statement inside an if_else statement, we call it nested _if_else statement.

syntax

if(condition/expression)
{
if(condition/expression)
{
Statement1;
}
else echo “Statement2”;
}
else echo “Statement2”;

Sample Program

In the above program, The first Condition is true, then it enters in 2nd condition to check and when the 2nd condition is true then it prints that statement and skips other else statements. See the Output below:

Click Here For Next Part – Difference between Single Equal, Double Equal and, Triple Equal in PHP