10-switch/index.php

Go to the documentation of this file.
00001 <?php
00013 define('CHOICE',"abcd");
00014 
00015 
00020 function main()
00021 {
00022 
00023     //Demonstrating simple switch case
00024     $choice1=CHOICE;
00025     switch($choice1)
00026     {
00027     /*  case 0: //Uncomment for suprise
00028                 echo "A. Zero";
00029                 break;*/
00030 
00031         case 1:  //intentional fall through
00032         case 2: 
00033                 echo 'A. $choice1 is either 1 or 2, that is ' . $choice1;
00034                 break;
00035     
00036         case "abcd":
00037                 echo 'A. PHP switch case can accept string constants as case values';
00038                 break;
00039 
00040         default:
00041                 echo 'A. If nothing else matches, there is always default :)';
00042                 break;
00043     }
00044 
00045     echo "<br/>\n<br/>\n";
00046 
00047 
00048     //Avoiding matching of any string with number zero by
00049     //converting input to string and matching against strings
00050     //instead of numbers.
00051     switch((string)$choice1)
00052     {
00053         case "0":
00054                 echo "B. Zero";
00055                 break;
00056 
00057         case "1":    //intentional fall through
00058         case "2": 
00059                 echo 'B. $choice1 is either 1 or 2, that is ' . $choice1;
00060                 break;
00061     
00062         case "abcd":
00063                 echo 'B. PHP switch case can accept string constants as case values';
00064                 break;
00065 
00066         default:
00067                 echo 'B. If nothing else matches, there is always default :)';
00068                 break;
00069     }
00070     
00071 }
00072 
00073 
00074 main();
00075 
00076 ?>

Generated on Fri Nov 4 14:16:54 2011 for PHP example documentation by  doxygen 1.4.7