Sunday, 31 July 2011

PHP Switch Statement

PHP Switch Statement

In the previous lessons we covered the various elements that make up an If Statement in PHP. However, there are times when an if statement is not the most efficient way to check for certain conditions.

For example we might have a variable that stores travel destinations and you want
to pack according to this destination variable. In this example you might have
20 different locations that you would have to check with a nasty long block
of If/ElseIf/ElseIf/ElseIf/... statements. This doesn't sound like much fun to code,
let's see if we can do something different.

PHP Switch Statement: Speedy Checking

With the use of the switch statement you can check for all these
conditions at once, and the great thing is that it is actually more efficient
programming to do this. A true win-win situation!

The way the Switch statement works is it takes a single variable as input
and then checks it against all the different cases you set up for that
switch statement. Instead of having to check that variable one at a time,
as it goes through a bunch of If Statements, the Switch statement only has to check
one time.



PHP Switch Statement Example


In our example the single variable will be $destination

and the cases will be: Las Vegas, Amsterdam, Egypt, Tokyo, and the Caribbean Islands.

PHP Code:

$destination = "Tokyo";
echo "Traveling to $destination<br />";
switch ($destination){
 case "Las Vegas":
  echo "Bring an extra $500";
  break;
 case "Amsterdam":
  echo "Bring an open mind";
  break; 
 case "Egypt":
  echo "Bring 15 bottles of SPF 50 Sunscreen";
  break; 
 case "Tokyo":
  echo "Bring lots of money";
  break;
 case "Caribbean Islands":
  echo "Bring a swimsuit";
  break; 
}

Display:

Traveling to Tokyo

Bring lots of money
The value of $destination was Tokyo, so when PHP performed the switch

operating on $destination in immediately did a search for a case with the
value of "Tokyo". It found it and proceeded to execute the code that existed
within that segment.

You might have noticed how each case contains a break; at the end of its code area. This
break prevents the other cases from being executed. If the above example did not
have any break statements then all the cases that follow Tokyo would have been executed as well.
Use this knowledge to enhance the power of your switch statements!
The form of the switch statement is rather unique, so spend some time reviewing it
before moving on. Note: Beginning programmers should always include the break; to
avoid any unnecessary confusion.

PHP Switch Statement: Default Case

You may have noticed the lack of a place for code when the
variable doesn't match our condition. The if statement has the else clause and
the switch statement has the default case.

It's usually a good idea to always include the default case in all your switch
statements. Below is a variation of our example that will result in none of the cases
being used causing our switch statement to fall back and use the default case. Note: the word
case does not appear before the word default, as default is a special keyword!


PHP Code:

$destination = "New York";
echo "Traveling to $destination<br />";
switch ($destination){
 case "Las Vegas":
  echo "Bring an extra $500";
  break;
 case "Amsterdam":
  echo "Bring an open mind";
  break;
 case "Egypt":
  echo "Bring 15 bottles of SPF 50 Sunscreen";
  break; 
 case "Tokyo":
  echo "Bring lots of money";
  break; 
 case "Caribbean Islands":
  echo "Bring a swimsuit";
  break;  
 default:
  echo "Bring lots of underwear!";
  break;

}

Display:

Traveling to New York

Bring lots of underwear!

Using PHP With HTML Forms

It is time to apply the knowledge you have obtained thus far and put it to real use. A very common application of PHP is to have an HTML form gather information from a website's visitor and then use PHP to do process that information. In this lesson we will simulate a small business's website that is implementing a very simple order form.

Imagine we are an art supply store that
sells brushes, paint, and erasers. To gather order information
from our prospective customers we will have to make a page with an HTML form
to gather the customer's order.

Note: This is an oversimplified example to educate you how to
use PHP to process HTML form information. This example
is not intended nor advised to be used on a real business website.

Creating the HTML Form

If you need a refresher on how to properly make an HTML form,
check out the HTML Form
Lesson
before continuing on.

We first create an HTML form that will let our customer choose
what they would like to purchase. This file should be saved
as "order.html"
.

order.html Code:

<html><body>
<h4>Tizag Art Supply Order Form</h4>
<form> 
<select> 
<option>Paint</option>

<option>Brushes</option>
<option>Erasers</option>
</select>
Quantity: <input type="text" /> 
<input type="submit" />
</form>

</body></html>

Display:

Tizag Art Supply Order Form


Quantity:


Remember to review HTML Forms
if you do not understand any of the above HTML code. Next we must alter
our HTML form to specify the PHP page we wish to send this information to. Also,
we set the method to "post".

order.html Code:

<html><body>

<h4>Tizag Art Supply Order Form</h4>
<form action="process.php" method="post"> 
<select name="item"> 
<option>Paint</option>
<option>Brushes</option>

<option>Erasers</option>
</select>
Quantity: <input name="quantity" type="text" /> 
<input type="submit" />
</form>
</body></html>


Now that our "order.html" is complete, let us continue on and
create the "process.php" file which will process the HTML form information.

PHP Form Processor

We want to get the "item" and "quantity" inputs that we have specified
in our HTML form. Using an associative array (this term is explained in the array lesson),
we can get this information from the $_POST associative array.

The proper way to get this information would be to create two new variables, $item
and $quantity and set them equal to the values that have been "posted". The name
of this file is "process.php".

process.php Code:

<html><body>
<?php
$quantity = $_POST['quantity'];
$item = $_POST['item'];

echo "You ordered ". $quantity . " " . $item . ".<br />";
echo "Thank you for ordering from Tizag Art Supplies!";

?>
</body></html>

As you probably noticed, the name in $_POST['name'] corresponds
to the name that we specified in our HTML form.

Now try uploading the "order.html" and "process.php" files to a PHP
enabled server and test them out. If someone selected the item brushes
and specified a quantity of 6, then the following
would be displayed on "process.php":

process.php Code:

You ordered 6 brushes.
Thank you for ordering from Tizag Art Supplies!

PHP & HTML Form Review

A lot of things were going on in this example. Let us step through
it to be sure you understand what was going on.

  1. We first created an HTML form "order.html" that had two input fields
    specified, "item" and "quantity".
  2. We added two attributes to the form tag to point to "process.php" and
    set the method to "post".
  3. We had "process.php" get the information that was posted
    by setting new variables equal to the values in the $_POST associative array.
  4. We used the PHP echo function to output the customers order.

Remember, this lesson is only to teach you how to use PHP to get information
from HTML forms. The example on this page should not be used for a real business.

No comments:

Post a Comment