Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 27th, 2012  |  syntax: None  |  size: 1.87 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Switch statement in PHP
  2. <?php
  3.  
  4.  
  5. switch($_POST["city"]){
  6.     case "sf":
  7.         echo "Welcome to San Francisco, enjoy the beautiful weather.";
  8.         break;
  9.     case "tokyo":
  10.         echo "Welcome to Tokyo, enjoy the sushi.";
  11.         break;
  12.     case "paris";
  13.         echo "Welcome to Paris, enjoy the Eiffel Tower.";
  14.         break;
  15.     Default:
  16.         echo "Please pick a city.";
  17.         echo "<a href="/week7.html">Go Back</a>";
  18.  
  19.  
  20. }
  21.  
  22. ?>
  23.        
  24. <!DOCTYPE html>
  25. <html>
  26.     <head>
  27.         <title></title>
  28.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  29.     </head>
  30.     <body>
  31.         <div>
  32.             <form action="Week7PHP.php" method="post">
  33.                 <input type="radio" name="city" value="sf">San Francisco<br />
  34.                 <input type="radio" name="city" value="paris">Paris<br/>
  35.                 <input type="radio" name="city" value="tokyo">Tokyo<br/>
  36.                 <input type="submit" id="Submit">
  37.             </form>
  38.         </div>
  39.     </body>
  40. </html>
  41.        
  42. <?php
  43. if(isset($_POST['city'])){
  44.     $city=$_POST['city'];
  45. }else{
  46.     $city='';
  47. }
  48. //Or simpler
  49. $city=(isset($_POST['city']))?$_POST['city']:'';
  50.  
  51.  
  52.  
  53. //The use $city
  54. switch($city){
  55.     case "sf":
  56.         echo "Welcome to San Francisco, enjoy the beautiful weather.";
  57.         break;
  58.     case "tokyo":
  59.         echo "Welcome to Tokyo, enjoy the sushi.";
  60.         break;
  61.     ...
  62.     ...
  63.  
  64. ?>
  65.        
  66. <?php
  67.  
  68.  
  69. switch($_POST["city"]){
  70.     case "sf":
  71.         echo "Welcome to San Francisco, enjoy the beautiful weather.";
  72.         break;
  73.     case "tokyo":
  74.         echo "Welcome to Tokyo, enjoy the sushi.";
  75.         break;
  76.     case "paris":
  77.         echo "Welcome to Paris, enjoy the Eiffel Tower.";
  78.         break;
  79.     default:
  80.         echo "Please pick a city.";
  81.         echo "<a href="/week7.html">Go Back</a>";
  82.         break;
  83.  
  84. }
  85.  
  86. ?>
  87.        
  88. if (isset($_POST["city")) {
  89.    /* switch statement */
  90. } else {
  91.    /* form element*/
  92. }