PHP Control Structure -if...else..elseif Statements

What is the meaning of Decision Making

In all programming languages, there are the biggest and important role of decision making. Just like you make a decision in real life, to do any work.You also have to use conditional structures in the programming language. Decision making is made up of two words: Decision and making. To become a successful developer in any programming language, you should understand the decision making. If condition is the most used condition and also another condition structure. Before making any application, its prototype is prepared. In the prototype, you decide what base your application and when to work. There is much conditional structure for creating an application.

In anoter word, decision making means you have to take a decision for your task complete. If you have need that my application print this statement when anyone user login and if the user does not enter password or userid so print another statement(enter your username and password).

In simple words, if your condition is true (according to your need ) then execute this code and if your condition is not true then your code is not executed ( or print another statement or execute another code )

There are various type of conditional structures.

  • if condition
  • If else statements
  • if...elseif....else statements (Nested if else)
  • Switch statements
  • Conditional structures are used in your code to make decisions.

    PHP if condition

    If statement is used when the one condition is true. (Only one statement ) If you want to use condition for one state, you can use if condition. To complete one task, you can use only if condition. If condition works when the condition is true. If the condition is not true , if statement does not work for your code. Use if condition for only one state that is true.

    Syntax of If condition

    The syntax below helps you understand the if condition.

    
    if (condition) {
       Statement (Code will be executed when condition is true);
    }
    		
    		
    

    In this example below, we will declare the value of a variable and create a condition. If the condition is true then some value will be return and if the condition is false then no value will be return.

    When the condition is true

    <?php
    $x = 20;
    
    if($x < "40")
    {
        echo "X is greater";
    }?>
    Run

    When the condition is not true

    <?php
    $x = 10;
    
    if ($x >"40") {
        echo "X is greater";
    }?>

    output-

    There will be nothing to display as output.

    As you can see the examples above. In the first example, if the condition is true, the code is executed. In the second example, if the condition is false, nothing is displayed as output, if the condition works on a single state which is a true statement.You can use if else condition or if else statement to handle the two states true and false.

    The if else Statements

    If else statement has two states. The first is true and the second is false. Just like there is only one state in the if condition. If statement executes code if the condition is true. If conditions become false then if statement will not produce any output. For the solution of this problem, we use the if else statement. In if else statement there are two types of states if one is true a statement(code) will be executed and another when condition is false then another statement will be executed .

    In simple word - When the condition is true a code will be executed and when the condition is false, another code will be executed.

    Syntax of if else statements

    if (condition)
    code to be executed if condition is true;
    else
    code to be executed if condition is false;

    The following example will output " Y is Greater" if the condition is true, otherwise(else) it will output "X is Greater ":

    Example

    <?php
    $x=30;
    $y=20;
    if ($x<$y)
    echo "Y is greater";
    else
    echo "X is Greater ";
    ?>
    Run

    PHP - The if..elseif...else Statement(Nested ifelse)

    If you have need to use two or more if else conditions then you can use the if..elseif...else Statement. In nested if else statements we can create more than one condition. However, in the If else statements we able to create only one condition. If the condition is true then the code will be executed and if the condition is false then the other code will be executed. But in nested if else statements you can use more than one condition.

    Syntax

    if (condition)
    
    if (condition)
    code to be executed if condition is true;
    elseif (condition)
    code to be executed if condition is true;
    else
    code to be executed if condition is false;
    

    The below example will output "Have a nice Friday " if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise, it will output "Have a good day.

    Example

    <?php
    $day="Fri";
    if ($day=="Fri")
    echo "Have a nice Friday ";
    
    elseif ($day=="Sun")
    echo "Have a happy Sunday";
    else
    echo "Have a good day";
    ?>
    Run

    In the nested if else, if a condition is matched then the compiler gets the exit from the block.