Tuesday, April 13, 2021
More

    Php Conditional Statements

    Must Read

    Programmerhttp://www.improgrammer.net
    We started this site to inspire young minds to motivate and encourage them towards Programming Language. In this site you will get programming tutorials, tech, programming facts, programming fun and programming blogs.

     

    Conditional statements are used to execute different events based on different conditions.

    You can use conditional statements in your code to make your decisions. PHP supports following four decision making statements:

    if statement:-
    executes some code only if a specified condition is true

    if…else statement:-
    use this statement if you want to execute a set of code when a condition is true and another if the condition is not true

    if…elseif….else statement:-
    is used with the if…else statement to execute a set of code if one of several condition are true

    switch statement:-
    is used if you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.


    PHP – The if Statement :

    The if statement is used to execute some code only if a specified condition is true.

     

    Syntax:

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

     

    Example:

    <?php
    $x=20;
    if(x<100)
    {
       value of x=$x;
    }
    ?>

    Output:
    value of x=20

     


    PHP – The if…else Statement :

    Use the if….else statement to execute some code if a condition is true and another code if the condition is false.

     

    Syntax:

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

     

    Example:

    <?php
    $x=20;
    if(x<100)
    {
      echo “value of x is less then 100”;
      echo “value of x=$x”;
    }
    else
    {
      echo “value of x is greater then 100”;
    }
    ?>

    Output:
    value of x is less then 100
    value of x=20

     


    PHP – The if…elseif….else Statement :

    Use the if….elseif…else statement to select one of several blocks of code to be executed.

    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:

    <?php
    $x=20;
    if(x<50)
    {
      echo “value of x is less then 50”;
      echo “value of x=$x”;
    }
    elseif(x<100)
    {
      echo “value of x is between 50-100”;
    }
    else
    {
      echo “value of x is greater then 100”
    }
    ?>

    Output:
    value of x is less then 50
    value of x=20

     


    PHP – The Switch Statement :

    If you want to select one of many blocks of code to be executed, use the Switch statement.

    The switch statement is used to avoid long blocks of if..elseif..else code.

     

    Syntax:

    switch (expression)
    {
       case label1:
          code to be executed if expression = label1;
          break;
       case label2:
          code to be executed if expression = label2;
          break;
       default:
          code to be executed if expression is different from both label1 and label2:
    }

     

    Example:

    The switch statement works in an unusual way. First it evaluates given expression then seeks a lable to match the resulting value. If a matching value is found then the code associated with the matching label will be executed or if none of the lables match then statement will will execute any specified default code.

    <?php
    $d=date(“D”);
    switch ($d)
    {
       case “Mon”:
          echo “Today is Monday”;
          break;
       case “Tue”:
          echo “Today is Tuesday”;
          break;
       case “Wed”:
          echo “Today is Wednesday”;
          break;
       case “Thu”:
          echo “Today is Thursday”;
          break;
       case “Fri”:
          echo “Today is Friday”;
          break;
       case “Sat”:
          echo “Today is Saturday”;
          break;
       case “Sun”:
          echo “Today is Sunday”;
          break;
       default:
          echo “which day is this ?”;
    }
    ?>

    Latest Articles

    More Recipes Like This