PHP if else Statements -Control Structure


What is the meaning of decision-making?

In all programming languages, there is the biggest and most 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 decision-making. If the condition is the most used condition and also another condition structure. Before making any application, a prototype is prepared. In the prototype, you decide what to base your application on and when to work. There is much conditional structure for creating an application.

In other words, decision-making means you have to make a decision for your task to be completed. If you have need that my application to print this statement when any user logs in, 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 types 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 one condition is true. (Only one statement ) If you want to use a 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, the statement does not work for your code. Use if the condition is true for only one state.

    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 the example below, we will declare the value of a variable and create a condition. If the condition is true, then some value will be returned, and if the condition is false, then no value will be returned.

    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 from 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 an if-else condition or if-else statement to handle the two states, true and false.

    The if-else Statements

    An 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 if 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 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 are 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 exits the block.


Please Share

Recommended Posts:-

Previous Posts:-

Featured Items:-


Certificate and Marksheet system with verification & Franchise in PHP

$48



Home Credito loan finance management multipurpose system in PHP

$65



Result management system with Marksheet in PHP website | PHP Scripts

$39





0 Comment