PHP/Statement/switch statement

Материал из Web эксперт
Перейти к: навигация, поиск

A switch Statement

   <source lang="html4strict">

<html> <head><title>A switch Statement</title></head> <body>

<?php

   $satisfied = "no";
   switch ( $satisfied ) {
     case "very":
       print "very";
       break;
     case "no":
       print "no";
       break;
     default:
       print "default";
   }

?>

</body> </html>

 </source>
   
  


A switch Statement for string

   <source lang="html4strict">

<html> <head> <title>A switch Statement</title> </head> <body> <?php $mood = "sad"; switch ( $mood ) {

   case "happy":
       print "I"m in a good mood";
       break;
   case "sad":
       print "Don"t be down!";
       break;
   default:
       print "Neither happy nor sad but $mood";

} ?> </body> </html>

      </source>
   
  


In a switch/case block, you specify what you are checking against, then give a list of possible values you want to handle.

   <source lang="html4strict">

<?php

           $Name = "Bob";
           switch($Name) {
           case "Jim":
                   print "Your name is Jim\n";
                   break;
           case "Sally":
                   print "Your name is Sally\n";
                   break;
           default:
                   print "I don"t know your name!\n";
           }
   ?>
 
 </source>
   
  


Omitting the break statement can be useful in some cases

   <source lang="html4strict">

<?

   $aColor = "Red";
   switch( $aColor )
   {
       case "red":
       case "Red":
           // The following prints if $aColor is
           // either "red" or "Red"
           print( "#FF0000
" ); break; case "green": case "Green": print( "#00FF00
" ); break; case "blue": case "Blue": print( "#0000FF
" ); break; default: print( "other
" ); break; }

?>

      </source>
   
  


Switch and constants

   <source lang="html4strict">

<?php define("ALIGN_LEFT", 1); define("ALIGN_CENTER", 2); define("ALIGN_RIGHT", 3); switch($value) {

 case ALIGN_LEFT :
   break;
 case ALIGN_CENTER :
   break;
 case ALIGN_RIGHT :
   break;

} ?>

 </source>
   
  


switch command for string value

   <source lang="html4strict">
<?php
 $choice = "Rock";
 switch ($choice){
    case "Pop":
        print "Pop not Rock";
        break;
    case "Rap":
        print "Rap and not Rock";
        break;
    case "Jazz":
        print "Jazz not Rock";
        break;
    case "Rock":
        print "Rock music";
        break;
 }
 ?>
          
      </source>
   
  


Switch Statement

   <source lang="html4strict">

<html>

<head>
 <title>Switch Statement</title>
</head>
<body>
<?php
 $num = 2;
 switch($num)
 {
   case 1 : echo("This is case 1 code"); break;
   case 2 : echo("This is case 2 code"); break;
   case 3 : echo("This is case 3 code"); break;
   default : echo("This is default code");  
 }
?>
</body>

</html>

 </source>
   
  


Use the "default" case

   <source lang="html4strict">

<?

   $nIndex = 17;
   switch ( $nIndex )
   {
       case 0:
           print( "zero
" ); break; case 1: print( "one
" ); break; case 2: print( "two
" ); break; default: print( "neither zero, one nor two
" ); break; }

?>

      </source>
   
  


Using endswitch to end the switch definition

   <source lang="html4strict">

<? $action= "ADD"; switch ($action):

   case "ADD":
      echo "Perform actions for adding.";
      break;
   case "MODIFY":
      echo "Perform actions for modifying.";
      break;
   case "DELETE":
      echo "Perform actions for deleting.";
      break;
   default:
      echo "Error: Action must be either ADD, MODIFY, or DELETE.";

endswitch; ?>

 </source>
   
  


Using switch to test for multiple values

   <source lang="html4strict">

<? $action = "ADD"; switch ($action) {

   case "ADD":
       echo "Perform actions for adding.";
       break;
   case "MODIFY":
       echo "Perform actions for modifying.";
       break;
   case "DELETE":
       echo "Perform actions for deleting.";
       break;

} ?>

 </source>
   
  


Using the DEFAULT: statement to generate an error

   <source lang="html4strict">

<? $action= "ADD"; switch ($action) {

   case "ADD":
       echo "Perform actions for adding.";
       break;
   case "MODIFY":
       echo "Perform actions for modifying.";
       break;
   case "DELETE":
       echo "Perform actions for deleting.";
       break;
   default:
       echo "Error: Action must be either ADD, MODIFY, or DELETE.";

} ?>

 </source>
   
  


Using the switch Statement

   <source lang="html4strict">

<?php

   $i = 0;
   switch($i) {
       case 1:
           echo "First case
"; case 2: echo "Second case
"; break; default: echo "Default case"; }

?>

 </source>
   
  


What happens when there are no break keywords

   <source lang="html4strict">

<? $action = "ORDER"; switch ($action) {

   case "ORDER":
       echo "order assembly.
"; case "PACKAGE": echo "packing.
"; case "SHIP": echo "shipping.
"; }

?>

 </source>