-->

  • PHP decision making if else elseif statements


    PHP decision making if else elseif statements




    Conditional statements are used to perform different actions based on different condtions. The if, elseif ...else statements are used to take decision based on the different condtion.

    You can use conditional statements in your code to make your decision. In PHP we have the following conditional statements:

    • if statement - executes some code if one condition is true






    • if...else statement - executes some code if a condition is true and another code if that condition is false
    • if...elseif...else statement - executes different codes for more than two conditions
    • switch statement - selects one of many blocks of code to be executed




    PHP - The if Statement


    The if statement executes some code if one condition is true.


    Syntax

    if (condition) {
      code to be executed if condition is true;
    }



    Example 



    $b = date("H");

    if ($b < "30") 
    {

      echo "Have a good day!";


    }


    ?>



    </body>
    </html>





    Have a good day!









    PHP - The if...Else Statement


    If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement. PHP if else elseif statements


    Syntax

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


    Example


    The following example will output "Have a nice weekend!" if the current day is Friday, Otherwise, it will output "Have a nice day!":


    Example 



       $d = date("D");
             
             if ($d == "Mon")
    {
                echo "Have a nice vacation!"; 
             } 
             else 
    {
                echo "Have a nice day!"; 
    }  


    ?>



    </body>
    </html>




    Have a nice vacation!





    The ElseIf Statement


    If you want to execute some code if one of the several conditions are true use the elseif statement.PHP decision making if else elseif statements


    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;



    Example


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



    Example 



       $d = date("D");
             
             if ($d == "Fri")
    {
                echo "Have a nice weekend!";
             }
             elseif  ($d == "Sun") {
                echo "Have a nice Sunday!"; 
             }
             else {
                echo "Have a nice day!"; 
          
      
    }

    ?>



    </body>
    </html>




    Have a nice day!




    PHP - The switch Statement


    The switch statement will be explained in the next chapter. ( link will be updated soon )



    Video on This Topic :- Soon


    I hope you liked this post, then you should not forget to share this post at all.
    Thank you so much :-)



  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.