The Switch Conditional

I l @ ve RuBoard

Once you get to the point where you have very elaborate if-elseif -else conditionals, you may find that it saves you time and clarifies your programming to use a switch conditional instead. The switch conditional takes only one possible condition and that is the value of a variable.

 switch ($Variable) {     case "value1":       statement(s)1;       break;    case "value2":       statement(s)2;        break;    default:       statement(s)3;       break; } 

It is critical that you comprehend how a switch conditional works. Starting at the beginning, once PHP finds the case that matches the value of the set variable, it will continue to execute statements until it either comes to the end of the switch conditional (the closing curly brace ) or hits a break statement, at which point it will exit the switch construct. Thus, it is imperative that you close every case (and even the default case, for consistency sake) with a break.

This above switch conditional is somewhat like a rewrite of this:

 if ($Variable == "value1") {     statement(s)1; }  elseif ($Variable=="value2") {     statement(s)2; }  else {     statement(s)3; } 

I'll explain: because the switch conditional uses the value of $Variable as its condition, it will first check to see if $Variable is equal to value1 and, if so, will execute statement(s)1. If not, it will check to see if $Variable is equal to value2, and, if so, will execute statement(s)2. If neither condition is met, the default action of the switch condition is to execute statement(s)3.

In the next section, The While Loop, you'll use switch in connection with a loop to create an HTML form that tells you how many days are in a month but to demonstrate switch 's capabilities here, you'll write a simple script that prints a message based upon what choice the user selects in an HTML form.

To use a switch conditional:

  1. Create a new HTML document in your text editor.

  2. Begin with the standard HTML header (Script 6.7):

    Script 6.7. This HTML form uses a pull-down menu to give the user a list of options (created by the SELECT input type).

    graphics/06sc07.jpg

     <HTML><HEAD><TITLE>HTML   Contact Form</TITLE></HEAD><BODY> 
  3. Make a form that takes some information from the user and gives them the option of how they want to be contacted.

     <FORM ACTION="HandleContact.php"  METHOD=POST> First Name <INPUT TYPE=TEXT NAME=  "FirstName" SIZE=20><BR> Last Name <INPUT TYPE=TEXT NAME=  "LastName" SIZE=20><BR> How would you prefer to be contacted:  <SELECT NAME="ContactHow"> <OPTION VALUE="">Select One:  </OPTION> <OPTION VALUE="Telephone">  Telephone</OPTION> <OPTION VALUE="Mail">Mail</OPTION> <OPTION VALUE="E-Mail">E-Mail  </OPTION> <OPTION VALUE="Fax">Fax</OPTION> </SELECT><BR> 

    I envision this as part of a feedback system for a larger Web application. This particular document and its handling page will constitute a couple of steps in the feedback process. Here the user will enter their name then choose how they want to be contacted.

  4. Complete the form with a comments box, then close the form, and the HTML document.

     Comments <TEXTAREA NAME="Comments"  ROWS=5 COLS=40></TEXTAREA><BR>  <INPUT TYPE=SUBMIT NAME="SUBMIT"  VALUE="Submit!"> </FORM></BODY></HTML> 
  5. Save your form as contact.html and upload it to your server.

  6. Now you'll need to create the page which will process one part of the contact.html page.

  7. Create a new PHP document in your text editor.

  8. Code the standard HTML header (Script 6.8).

     <HTML><HEAD><TITLE>Contact  Information Request</TITLE><BODY> 
    Script 6.8. The switch conditional in this script uses the value of $ContactHow to determine what to request from the user: telephone number, fax number, E-mail address, or mailing address. HIDDEN input types are also utilized to pass along other existing values.

    graphics/06sc08.jpg

    graphics/06sc08a.jpg

  9. Create an HTML form and then open the PHP section.

     <FORM ACTION="HandleContact2.php"  METHOD=POST><?php 

    In order to retrieve more information from the user and to gather more, you'll use another form.

  10. Store the values retrieved from contact.html in HIDDEN form elements.

     print ("<INPUT TYPE=HIDDEN   NAME=\"FirstName\"   VALUE=\"$FirstName\">\n");  print ("<INPUT TYPE=HIDDEN   NAME=\"LastName\"  VALUE=\"$LastName\">\n");  print ("<INPUT TYPE=HIDDEN   NAME=\"Comments\"   VALUE=\"$Comments\">\n");  print ("<INPUT TYPE=HIDDEN   NAME=\"ContactHow\"   VALUE=\"$ContactHow\">\n"); 

    I mentioned in Chapter 3, HTML Forms and PHP, that you could pass along variable information using the HIDDEN INPUT type. Here the PHP will place all of the data collected from contact.html into hidden elements so that the data continues to be passed along to the next page HandleContact2.php.

  11. Create a switch conditional that reacts differently based upon which contact option the user selected in contact.html.

     switch ($ContactHow) { case "Telephone": print("<B>Please enter a daytime   phone number where you can be   reached:</B><BR>\n"); print ("<INPUT TYPE=TEXT NAME=   \"Telephone\" SIZE=10><BR>v"); print ("<INPUT TYPE=SUBMIT NAME=   SUBMIT VALUE=\"Continue\">\n"); break; case "Mail": print("<B>Please enter your complete   mailing address:</B><BR>\n"); print ("<TEXTAREA NAME=   \"MailAddress\" ROWS=5 COLS=40>   </TEXTAREA><BR>\n"); print ("<INPUT TYPE=SUBMIT NAME=   SUBMIT VALUE=\"Continue\">\n"); break; case "E-Mail": print("<B>Please enter your E-Mail   address:</B><BR>\n"); print ("<INPUT TYPE=TEXT NAME=   \"E-Mail\" SIZE=40><BR>\n"); print ("<INPUT TYPE=SUBMIT NAME=   SUBMIT VALUE=\"Continue\">\n"); break; case "Fax": print("<B>Please enter your Fax   number:</B><BR>\n"); print ("<INPUT TYPE=TEXT NAME=   \"Fax\" SIZE=10><BR>\n"); print ("<INPUT TYPE=SUBMIT NAME=   SUBMIT VALUE=\"Continue\">\n"); break; default: print("<B>Please go back and select   how you would prefer to be   contacted!</B><BR>\n"); break; } 

    This switch conditional is set up to print out different things depending upon the value of $ContactHow. In short, if they want to be telephoned, the page will request a phone number, if they prefer E-mail, an e-mail address is requested , and so forth. If $ContactHow has no value, the default case will be triggered and a message requesting they return to the contact.html page to select a contact method is printed.

  12. Close the PHP section, then the form, and the HTML page.

     ?></FORM></BODY></HTML> 
  13. Save your script as HandleContact.php, upload it to your server (in the same directory as contact.html ) and test both pages in your Web browser (Figures 6.10, 6.11, 6.12, 6.13, and 6.14).

    Figure 6.10. Here is the HTML form created by contact.html. It constitutes step one of a feedback system.

    graphics/06fig10.gif

    Figure 6.11. Because I requested that I be contacted via E-mail (see Figure 6.10), HandleContact.php will now request an E-mail address and display a TEXT input type where I can enter that information.

    graphics/06fig11.gif

    Figure 6.12. This is the source of what you see in Figure 6.11. Notice that the HIDDEN fields are storing the information already gathered so that too will be passed on to HandleContact2.php.

    graphics/06fig12.jpg

    Figure 6.13. If I decided that I wanted to be contacted by standard mail, HandleContact.php would request a mailing address and this time provide a large TEXTAREA where I can enter that information.

    graphics/06fig13.gif

    Figure 6.14. If the user does not select a preference for how they want to be contacted, they will see this message and not be allowed to continue on.

    graphics/06fig14.gif

Tip

I have not created a HandleContact2.php page which handles the results of this HandleContact.php page (as I mentioned, I was only going to develop a couple of steps in a larger process). If you want to make sure that HandleContact.php works as it should, write a simple PHP script that prints out all the array values.


Tip

A default case is not required in your switch conditional (you could set it up so that if the value is not explicitly met by one of the cases, nothing happens), but if used, it must be listed as the last case.


Tip

If you are using a string in your switch conditional, keep in mind that it is case-sensitive, meaning that "Value" will not match a string "value".


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net