1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Decision control statements in PHP

Discussion in 'PHP' started by MinalS, Nov 9, 2015.

  1. The following conditional statements are used in PHP.
    1. if statement
    2. if ….else statement
    3. if….elseif…else statement
    4. switch statement

    1. if statement



    The statement is executed only if the condition is true.

    Syntax:

    There are various ways to represent if statement in PHP. They are as mentioned below:

    Code:
    1) if ( condition) 
        Statements are defined
    2) if ( condition ) {
        Statements to be executed
    }
    3) if ( condition ) :
        code is executed if the condition is satisfied
    endif ;
    
    Example:

    Code:
    <body>
        <?php
            $x = 5;
            $y = 7;
            if ( $x < $y):
                echo "x is less than y";
            endif;
        ?>
    </body>
    

    2. if …else statement



    The statement is used to execute a code when the specified condition is true. The other set of statements are executed if the condition is false.

    Syntax:

    Various ways to define the if … else statement are as mentioned below:

    Code:
    1) 
    if ( condition ):
        code is executed if the condition is satisfied
    else
        code is executed if the condition is not satisfied
        endif;
    2)
     if ( condition ) 
        Statements to be executed
       else 
        Condition is not satisfied, statements are executed
    3) 
    if ( condition )
    {
        Statements are executed
    }
    else
    {
        Statements are processed
    }
    
    Example:

    Code:
    <body>
        <?php 
            $x = 10;
            $y = 20;
            if ( $x == $y ) :
                 echo "x and y have same values";
            else:
                 echo "x and y are not equal";
            endif;
        ?>
    </body>
    

    3. if…elseif…else statement



    The if….elseif…else statement is used for checking many conditions. A new condition is defined if the first condition is not satisfied.

    Syntax:

    The alternative syntax which can be used for the if …elseif statement are mentioned below:

    Code:
    1)
    if ( condition ):
        code is executed if the condition is satisfied;
    elseif ( condition):
        Code is executed if the condition is satisfied
    else:
        Code is executed if the condition is not satisfied
        endif;
    
    2) 
    
        if ( condition )
        code is executed if the condition is satisfied;
    elseif ( condition)
        Code is executed if the condition is satisfied
    else    Code is executed if the condition is not satisfied
    
    3)
    
    if ( condition )
    {
        code is executed if the condition is satisfied;
    }
    elseif ( condition){
        Code is executed if the condition is satisfied
    }
    else{
        Code is executed if the condition is not satisfied
        }
    
    
    Example:

    Code:
    <body>
        <?php 
            $x = 20;
            $y = 25;
            if ( $x > $y ):
                echo "x is greater than y";
            elseif ($x < $y):
                echo "x is les than y";
            else:
                echo "x and y are equal";
            endif; 
        ?>
    </body>
    
    

    4. Switch statement



    When user needs to check multiple conditions, the switch statement is used. Using the switch statement, an expression is evaluated and the value is matched with the condition defined in the case label.

    Syntax:

    The various ways in which the switch statement is executed is as shown below:

    Code:
    1)
        switch ( expression ):
            case value_1:
                statement1;
                [ break; ]
            case value_2:
                statement 2;
                [ break; ]
            ……
            default: 
                statement _ n;
                break;
        endswitch;
    2)
    switch ( expression )
            {
            case value_1:
                statement1;
                [ break; ]
            case value_2:
                statement 2;
                [ break; ]
            ……
            default: 
                statement _ n;
                break;
            }
    3)
    switch ( expression )
            case value_1:
                statement1;
                [ break; ]
            case value_2:
                statement 2;
                [ break; ]
            ……
            default: 
                statement _ n;
                break;
    
    Example:

    Code:
    <?php
        $grade = "excellent";
        switch ( $grade ):
            case "good":
                echo "Grade is good";
                break;
            case "fair":
                echo "Grade is fair";
                break;
            case "excellent":
                echo "Grade is excellent";
                break;
            default:
                echo "Not a valid grade";
                break;
        endswitch;
    ?>
    
     

Share This Page