A program begins running, might ask the user some questions, responds to these inputs, and continues interacting with the user until he indicates an interest in leaving the program.. Whe
Trang 1Building the Place Array
I notice that each place is a string value associated with some number I use the
array()directive to preload the $placearray with appropriate values Zero has no
corresponding place, so I simply left the 0element blank
$place = array(
“”,
“on my thumb”,
“on my shoe”,
“on my knee”,
“on a door”);
Like most places in PHP, carriage returns don’t matter when you’re writing the
source code I put each place on a separate line, just because it looked neater that
way
Writing Out the Lyrics
The song itself is incredibly repetitive Each verse is identical except for the verse
number and place For each verse, the value of the $versevariable is the current
verse number The corresponding place is stored in $place[$verse] A large print
statement in a forloop prints the entire code
//print out song
for ($verse = 1; $verse <= 4; $verse++){
print <<<HERE
This old man, He played $verse
He played knick-knack $place[$verse]
with a knick, knack, paddy-whack
give a dog a bone
This old man came rolling home
HERE;
} // end for loop
The Fancy Old Man program illustrates very nicely the tradeoff associated with
using arrays Creating a program that uses arrays correctly often takes a little more
planning than using control structures alone (as in This Old Man) However, the
extra work up front pays off because the program is easier to modify and extend
113
Trang 2l u
Keeping Persistent Data
Most traditional kinds of programming presume that the user and the program are engaging in a continual dialog A program begins running, might ask the user some questions, responds to these inputs, and continues interacting with the user until he indicates an interest in leaving the program
Programs written on a Web server are different The PHP programs you are writ-ing have an incredibly short life span When the user makes a request to your PHP program through a Web browser, the server runs the PHP interpreter(the pro-gram that converts your PHP code into the underlying machine language your server understands) The result of the program is a Web page that is sent back to the user’s browser Once your program sends a page to the user, the PHP program shuts down because its work is done Web servers do not maintain contact with the browser after sending a page Each request from the user is seen as an entirely new transaction
The Poker Diceprogram at the beginning of this chapter appears to interact with the user indefinitely Actually, the same program is being called repeatedly The program acts differently in different circumstances Somehow it needs to keep track of what state it’s currently in
Counting with Form Fields
You can store information a couple of ways, including files, XML, and databases The second half of this book details these important ideas The easiest approach
to achieving data permanence is to hide the data in the user’s page To illustrate, take a look at Figures 4.9 and 4.10
I N THE R EAL W ORLD
The underlying Web protocol (HTTP) that Web servers use does not keep con-nections open any longer than necessary This behavior is referred to as being a stateless protocol Imagine if your program were kept running as long as any-body anywhere on the Web were looking at it What if a person fired up your program and went to bed? Your Web server would have to maintain a connec-tion to that page all night Also remember that your program might be called by thousands of people all at the same time
It can be very hard on your server to have all these concurrent connections open Having stateless behavior improves your Web server’s performance, but that per-formance comes at a cost Essentially, your programs have complete amnesia every time they run You need a mechanism for determining the current state.
Trang 3Each time you click the Persistenceprogram’s submitbutton, the counters
incre-ment by one The program behavior appears to contradict the basic nature of
server-side programs because it seems to remember the previous counter value
In fact, if two users were accessing the Persistenceprogram at the same time,
each would count correctly Look at the source code to see how it works:
115
FIGURE 4.9
The program has
two counters that
read 1 when the
program is run
the first time.
FIGURE 4.10
Both values are
incremented after
the user clicks the
submit button.
Trang 4<head>
<title>
persistence demo
</title>
</head>
<body>
<h1>Persistence Demo</h1>
<form>
<?
//increment the counters
$txtBoxCounter++;
$hdnCounter++;
print <<<HERE
<input type = “text”
name = “txtBoxCounter”
value = “$txtBoxCounter”>
<input type = “hidden”
name = “hdnCounter”
value = “$hdnCounter”>
<h3>The hidden value is $hdnCounter</h3>
<input type = “submit”
value = “click to increment counters”>
HERE;
?>
</form>
</body>
</html>
Storing Data in the Text Box
The program has two variables: $txtBoxCounter and $hdnCounter For now, con-centrate on $txtBoxCounter, which is related to the text box When the program
116
l u
Trang 5begins, it grabs the value of $txtBoxCounter(if it exists) and adds one to it When
the program prints the text box, it automatically places the $txtBoxCountervalue
in the text box
Since the form has no action attribute defined, the program automatically calls
itself when the user clicks the submit button This time, $txtBoxCounter has a
value (1) When the program runs again, it increments $txtBoxCounterand stores
the new value (now 2) in the text box Each time the program runs, it stores in
the text box the value it needs on the nextrun
Using a Hidden Field for Persistence
The text box is convenient for this example because you can see it, but using a
text box this way in real programs causes serious problems Text boxes are
editable by the user, which means she could insert any kind of information and
really mess up your day
Hidden form fields are the unsung heroes of server-side programming Look at
$hdnCounterin the source code This hidden field also has a counter, but the user
never sees it However, the value of the $hdnCounter variable is sent to the PHP
program indicated by the form’s actionattribute That program can do anything
with the attribute, including printing it in the HTML code body
Very often when you want to track information between pages, you store the
information in hidden fields on the user’s page
The hidden fields technique shown here works fine for storing small amounts of information, but it is very inefficient and insecure when you are working with more serious forms of data
Writing the Poker Dice Program
It’s time to take another look at the Poker Diceprogram that made its debut at the
beginning of this chapter As usual, this program doesn’t do anything you haven’t
already learned It is a little more complex than the trivial sample programs I show
you in this chapter, but it’s surprisingly compact considering how much it does It
won’t surprise you that arrays and loops are the secret to this program’s success
Setting Up the HTML
As always, a basic HTML page serves as the foundation for the PHP program I add
a simple style sheet to this page to make tan characters on a green background
T R A P
117