PHP - Conditional Statements

    PHP - Conditional statements

    Conditional statements are used whenever we work on some conditions to execute a particular set of instructions if those conditions are set to be true. These statements control the flow of execution within the program based on the desired conditions. There are three decision making statements that are supported by PHP.

    • If statement
    • If-else statement
    • If- Elseif-else statement
    • Switch statement

    1. If statement

    This statement will check for the given condition if the condition gives true result then the following set of instructions will get executed. Syntax

    <?php
    if(condition)
    {
    //given code will be executed if the above mentioned condition is true
    }
    ?>

    Example

    <?php
    if(2>0)
    {
    Echo “hello”;
    ?>

    Output hello

    2. If- else statement

    With this expression, you will get to test a given condition if the result of the condition is true then the following set of statements will get executed. In case if you want to perform some action even if the condition is false then the statements will be included within the else block. Syntax

    <?php
    if(condition)
    {
    //code will  be executed if the above-mentioned condition is true
    }
    Else
    {
    //execute this code block if the given condition is false
    }
    >?

    Example

    <?php
    if(2<0)
    {
    Echo “hello”;
    }
    Else
    {
    Echo “bye”;
    }
    ?>

    Output bye

    3. If-elseif-else statement

    In this expression, you will get to check more than one condition to execute some instruction if any of the conditions proves to be true. If none of the conditions comes out to be true then the else block which is mostly defined at the end of the program will get executed. Syntax

    <?php
    if(condition 1)
    {
    //code to be executed if condition 1 is true.
    }
    Elseif (condition 2)
    {
    //code to be executed if condition 2 is true.
    }
    Elseif (condition 3)
    {
    //code to be executed if condition 3 is true.
    }
    Else
    {
    //code to be executed if none of the above conditions is true.
    }
    ?>

    Example-

    <?php
    if(2<0)
    {
    Echo “hello”;
    }
    Elseif (3<0)
    {
    Echo “bye”;
    }
    Else
    {
    Echo “this is right”;
    }
    ?>

    Output This is right

    4. Switch statement

    Using this statement you can avoid the long if-elseif-else block of code. With this statement, you are able to execute many blocks at a time having a different condition to be checked. Syntax

    </php
    switch (expression){
       case label1:
         // code to be executed if expression matches label1;
          break;  //to end the case label 1 block
       case label2:
          //code to be executed if expression matches label2;
          break;
          default:// works like the else block of if-elseif-else.  
    }
    ?>
    

    Example

    <?php
    if(2<0)
    {
    Echo “hello”;
    }
    Elseif (3<0)
    {
    Echo “bye”;
    }
    Else
    {
    Echo “this is right”;
    }
    ?>

    Output

    This is right

    People are also reading: