Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

PHP if else Statements -Control Structure


What is the meaning of Decision Making

In all programming languages, there is 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 the 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 other words, decision making means you have to take a decision for your task to complete. If you have need that my application print this statement when anyone user login and if the user does not enter the password or user id 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 the statement is used when one condition is true. (Only one statement ) If you want to use condition for one state, you can use it if condition. To complete one task, you can use only if condition. If the condition works when the condition is true. If the condition is not true if the statement does not work for your code. Use if the condition for only one state that is true.

    Syntax of PHP If condition

    The syntax below helps you understand the if condition.

    
    if (condition) {
       Statement (Code will be executed when the 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 the 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 the statement executes code if the condition is true. If conditions become false then if the statement will not produce any output. For the solution of this problem, we use the if-else statement. In an if-else statement there are two types of states if one is true a statement(code) will be executed and another when the condition is false then another statement will be executed .

    In simple words - 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

    If you have need to use two or more if else conditions then you can use the if..elseif...else Statement. In if...elseif...else statement, 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)
    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.


    Please Share

    Recommended Posts:-

    Previous Posts:-

    Featured Items:-


    Payment form by PayUmoney gateway with GST invoice,email | PHP software

    $38



    Modern blog CMS in PHP with MYSQL database | PHP blog scripts

    $67



    Integrate Payumoney payment gateway in PHP website | Source Scripts

    $12




    0 Comment