Tuesday, 11 August 2015

PHP Decision Making Concepts :-

  • The if, elseif ...else and switch statements are used to take decision based on the different condition.
  • PHP supports following three decision making statements:
    • if...else statement :-  This statement is used if we want to execute a set of code when a condition is true and another if the condition is not true. 
              Syntax:-
       
              if (condition)
                  code to be executed if condition is true;
              else
                  code to be executed if condition is false;

            Example :-

            if..else.php :-
            <html>
             <body>
             <?php
              $y=1;
             if($y==1)
             echo "<b> Y's value is displayed </b>";
             else
             echo "<b> Y's value is not displayed </b>";
            ?>
           </body>
           </html>

             Screenshot :-
             
              
    • elseif statement :- It is used with the if...else statement to execute a set of code if one of several conditions are true.
              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:-

                elseif.php
                <html>
                 <body>
                 <?php
                   $x=date("D");
                    echo "<b>Today is :- $x </b> <br/>";

                  if ($x=="Fri")
                  echo "So, have a nice weekend!";
                   elseif ($x=="Sun")
                   echo "So, have a nice Sunday!";
                   else
                   echo "So, have a nice day!";
                    ?>
                  </body>
                  </html>

                  Screenshot :-
                   

    • switch statement :-  It is used if we 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:-

              switch.php:-
               <html>
                <body>
               <?php
               $d=date("D");
               echo "<b>Today is :- $d </b> <br/>";

                   switch ($d)
                   {
                  case "Mon":
                  echo "So, today is Monday";
                  break;

                  case "Tue":
                  echo "So, today is Tuesday";
                   break;

                  case "Wed":
                  echo "So, today is Wednesday";
                  break;

                  case "Thu":
                  echo "So, today is Thursday";
                   break;

                   case "Fri":
                   echo "So, today is Friday";
                   break;

                   case "Sat":
                   echo "So, today is Saturday";
                   break;

                   case "Sun":
                   echo "So, today is Sunday";
                   break;

                   default:
                  echo "oh,ho!!! None of the above";
                 }
                 ?>
                </body>
                 </html>

                  Screenshots :-
                  

    No comments:

    Post a Comment