Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [212]

By Root 1536 0
” xml:lang=”en”>

ownForm.php

if (isset($_REQUEST[“userName”])){

$userName = $_REQUEST[“userName”];

print “

Hi, $userName

\n”;

} else {

print <<

method = “post”>

name = “userName”>

HERE;

} // end if

?>

Making a program “do its own stunts” like this is pretty easy. The key is using an if statement. However, begin by thinking about the behavior. In this example, the program revolves around the $userName variable. If this variable has a value, it can be processed. If the variable has not been set yet, the user needs to see a form so she can enter the data.

1. Check for the existence of a key variable.

Use the isset() function to determine whether the variable in question has been set. Check the $_REQUEST or one of the other superglobals ($_POST or $_GET) to determine whether the form has already been submitted. You need to check the existence of only one variable, even if the form has dozens.

2. If the variable exists, process the form.

If the variable exists, extract all the variables from the form and carry on with your processing.

3. If the variable does not exist, build the form.

If the variable does not exist, you need to make the form that will ask the user for that variable (and any others you need). Note that the action attribute of the form element should be null (“”). This tells the server to re-call the same program.


Making a switch

Often, you will run across a situation where you have one expression that can have many possible values. You can always use the if – elseif structure to manage this situation, but PHP supplies another interesting option, shown in Figure 3-7.

Figure 3-7: The Magic 8 Ball uses a switch.

The code for this program uses the switch structure. Take a look at how it’s done:

switch.php

Ask the magic 8 ball a yes or no question!

$yourNumber = rand(1,8);

switch($yourNumber){

case 1:

print ”

\”fat

”;

break;

case 2:

print ”

\”Yes\”

”;

break;

case 3:

print ”

\”PhD\”

”;

break;

case 4:

print ”

\”You

”;

break;

case 5:

print ”

\”tell,

”;

break;

case 6:

print ”

\”Why

”;

break;

case 7:

print ”

\”Ask

”;

break;

case 8:

print ”

\”The

”;

break;

default:

print ”

An error has occurred. Please try again, or contact support@somesite.com for assistance. Error code: 8BIC:$yourNumber

”;

}

?>

Ask another question!

The main (in fact nearly only) feature of this code is the switch statement. Here’s how it works:

1. Begin with the switch statement.

This indicates that you will be building a switch structure.

2. Put the expression in parentheses.

Following the switch statement is a pair of parentheses. Put the expression (usually a variable) you wish to evaluate inside the parentheses. In this case, I’m checking the value of the variable $yourNumber.

3. Encase the entire switch in braces.

Use squiggle braces to indicate the entire case. As in most blocking structures, use indentation to help

Return Main Page Previous Page Next Page

®Online Book Reader