×
☰ See All Chapters

PHP Conditional Statements

By default all statements are executed sequentially in PHP program. Control statements are used to change the execution of statements based on conditions.

In PHP we have the following conditional statements:

  • if 

  • if else 

  • else if 

  • switch 

if statement

if statement syntax:

if(boolean_condition)

{

/* statement(s) will execute if the boolean condition is true */

}

If the boolean_condition evaluates to true, then statements inside if block will be executed. If the boolean_condition evaluates to false, then statement immediately after if block will be executed. Statement inside if block may be a single statement or a compound statement enclosed in curly braces (that is, a block). If the statement inside if block is single statement the curly braces are optional.

if else statement

if else statement syntax:

if(boolean_condition)

{

/* statement(s) will execute if the boolean condition is true */

}

else {

/* statement(s) will execute if the boolean condition is false */

}

else block is optional. If the boolean_condition evaluates to true, then the if block will be executed, otherwise else block will be executed.

if-else-if Ladder

if-else-if ladder syntax:

if(boolean_condition 1) {

/* Executes when the boolean condition 1 is true */

}

else if( boolean_condition 2){

/* Executes when the boolean condition 2 is true */

}

else if( boolean_condition 3){

/* Executes when the boolean condition 3 is true */

}

else {

/* executes when the none of the above condition is true */

}

else if and else blocks are optional. If there are number of operations need to be done based on certain conditions then if else ladder is useful. Ladder will be executed from top till any boolean_condition becomes true. If any boolean_condition evaluates to true then that block will be executed. If no boolean_condition evaluates to true then last else block will be executed.

Nested ifs

Nested ifs  syntax:

if( boolean_condition 1)

{

     /* Executes when the boolean condition 1 is true */

        if(boolean_condition 2)

        {

        /* Executes when the boolean condition 2 is true */

        }

}

We can have if, if else, if else ladder inside any if, if else, if else ladder.

switch statement

switch statement syntax:

switch (expression) {

case value_expression:

        statement(s);

        break; /* optional */

case value_expression:

        statement(s);

        break; /* optional */

/* you can have any number of case statements */

default: /* Optional */

        statement(s);

}

switch statement is used to execute a particular set of statements among multiple conditions. It is an alternative to complicated if else if ladder conditions.

  • When switch block execution starts, this expression will be evaluated to a value. 

  • The value_expression should be evaluated to a constant value of type same as the expression type in the switch.  

  • Case block whose value is equal to the switch expression will be executed. 

  • break is optional. Every case must end with break statement which will help to terminate and transfer the control outside of switch block. If no break is used then execution will continue to next case. 

  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.  

Nested switch statements

We can have switch blocks inside other switch block.

Nested switch statements Syntax:

switch (expression) {

case value_expression:

        switch (expression) {

        case value_expression:

                statement(s);

                break;

        case value_expression:

                statement(s);

                break;

        default:

                statement(s);

        }

        break;

case value_expression:

        statement(s);

        break;

default:

        statement(s);

}

 

 


All Chapters
Author