Switch Statement in Php

The switch statement is used to avoid complex nested if else conditions. The structure of the switch statement is very simple we just pass a variable to the switch and test it in different cases. Each case end with break statement and each new case start with new number.

Source Code

<html>
 <head>
  <title>For Loops  </title>
 </head>
 <body> 
 <?
  echo "<br />";
  echo " For Loop _ 1 : ";
  echo "<br />";
  echo "<br />";
  for ($Numbers=0; $Numbers <= 10; $Numbers++) {
   echo $Numbers . ", ";
  }
  echo "<br />";
  echo "<br />";
  echo " For Loop _ 2 : ";
  echo "<br />";
  echo "<br />";
  for ($Numbers=10; $Numbers <= 20; $Numbers++) {
   echo $Numbers . ", ";
  }
  echo "<br />";
  echo "<br />";
  echo " For Loop _ 3 : ";
  echo "<br />";
  echo "<br />";
  for ($Numbers=20; $Numbers <= 30; $Numbers++) {
   echo $Numbers . ", ";
  }
     echo "<br />";
  echo "<br />";
  echo " For Loop _ 4 : ";
  echo "<br />";
  echo "<br />";
  for ($Numbers=30; $Numbers <= 40; $Numbers++) {
   echo $Numbers . ", ";
  }
 ?>
 </body>
</html><html>
 <head>
  <title>Logical Expressions: Switch</title>
 </head>
 <body>
  <?php 
   /* switch
   Useful when there are many possible actions based on the value of single variable
   */
   
   $a = 2;
  
   switch ($a) {
    case 0: 
     echo "a equals 0";
     break;
    case 1: 
     echo "a equals 1";
     break;
    case 2: 
     echo "a equals 2";
     break;
    default:
     echo "a is not 0, 1, or 2";
     break;
   }
  ?>
 </body>
</html>

Output of the Program 


Switch Statement in Php

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment