Tải bản đầy đủ (.pdf) (5 trang)

PHP 5/MySQL Programming- P27 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (122.55 KB, 5 trang )

First look over the entire program, then see how it does its work.
<html>
<head>
<title>
Basic Array
</title>
</head>
<body>
<h1>Basic Array</h1>
<?
//simply assign values to array
$camelPop[1] = “Somalia”;
$camelPop[2] = “Sudan”;
$camelPop[3] = “Mauritania”;
$camelPop[4] = “Pakistan”;
$camelPop[5] = “India”;
//output array values
print “<h3>Top Camel Populations in the World</h3>\n”;
for ($i = 1; $i <= 5; $i++){
print “$i: $camelPop[$i]<br>\n”;
} // end for loop
print “<i>Source: <a href = />Food and Agriculture Organization of the United Nations</a></i>\n”;
//use array function to load up array
$binary = array(“000”, “001”, “010”, “011”);
print “<h3>Binary numbers</h3>\n”;
for ($i = 0; $i < count($binary); $i++){
print “$i: $binary[$i]<br>\n”;
} // end for loop
?>
</body>
</html>


108
P
H
P
5
/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s

o
l
u
t
e
B
e
g
i
n
n
e
r
Generating a Basic Array
Look at the lines that describe $camelPop:
//simply assign values to array
$camelPop[1] = “Somalia”;
$camelPop[2] = “Sudan”;
$camelPop[3] = “Mauritania”;
$camelPop[4] = “Pakistan”;
$camelPop[5] = “India”;
The $camelPop variable is a variable meant to hold the five countries with the
largest camel populations in the world. (If this array stuff isn’t working for you,
at least you’ve learned something in this chapter!) Since
$camelPop is going to
hold the names of five different countries, it makes sense that this is an
array
(computer geek lingo for list) rather than an ordinary variable.
The only thing different about
$camelPop and all the other variables you’ve

worked with so far is
$camelPop can have multiple values. To tell these values
apart, use a numeric index in square brackets.
Apparently the boxer George Foreman has several sons also named George. I’ve
often wondered what Mrs. Foreman does when she wants somebody to take out
the trash. I suspect she has assigned a number to each George, so there is no
ambiguity. This is exactly how arrays work. Each element has the same name, but
a different numerical index so you can tell them apart.
Many languages require you to explicitly create array variables, but PHP is very
easygoing in this regard. Simply assign a value to a variable with an index in
square brackets and you’ve created an array.
Even though PHP is good natured about letting you create an array variable on-
the-fly, you might get a warning about this behavior on those Web servers that
have error reporting set to E_ALL. If that’s the case, you can create an empty array
with the array() function described in the following sections and then add
values to it.
Using a Loop to Examine an Array’s Contents
Arrays go naturally with for loops. Very often when you have an array variable,
you step through all of its values and do something to each one. In this example,
I want to print the index and the corresponding country’s name. Here’s the
for
loop that performs this task:
TRICK
TRICK
109
C
h
a
p
t

e
r 4 L
o
o
p
s
a
n
d
A
r
r
a
y
s
//output array values
print “<h3>Top Camel Populations in the World</h3>\n”;
for ($i = 1; $i <= 5; $i++){
print “$i: $camelPop[$i]<br>\n”;
} // end for loop
Because I know the array indices will vary between 1 and 5, I set up my loop so the
value of
$i will go from 1 to 5. Inside the loop, I simply print the index ($i) and the
corresponding country (
$camelPop[$i]). The first time through the loop, $i is 1, so
$camelPop[$i] is $camelPop[1], which is Somalia. Each time through the loop, the
value of
$i is incremented, so eventually every array element is displayed.
The advantage of combining loops and arrays is convenience. If you want to do
something with each element of an array, you only have to write the code one time,

then put that code inside a loop. This is especially powerful when you start
designing programs that work with large amounts of data. If, for example, I want
to list the camel population of every country in the UN database rather than
simply the top five countries, all I have to do is make a bigger array and modify the
for loop.
Using the array() Function to Preload an Array
Often you start out knowing exactly which values you want placed in an array.
PHP provides a shortcut for loading an array with a set of values.
//use array function to load up array
$binary = array(“000”, “001”, “010”, “011”);
In this example, I create an array of the first four binary digits (starting at zero).
The
array keyword can assign a list of values to an array. Note that when you use
this technique, the indices of the elements are created for you.
Most computer languages automatically begin counting things with zero rather
than one (the way humans tend to count). This can cause confusion. When PHP
builds an array for you, the first index is 0 automatically, not 1.
Detecting the Size of an Array
Arrays are meant to add flexibility to your code. You don’t actually need to know
how many elements are in an array, because PHP provides a function called
count(), which can determine how many elements an array has. In the following
code, I use the
count() function to determine the array size:
TRAP
TRICK
110
P
H
P
5

/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e

B
e
g
i
n
n
e
r
111
C
h
a
p
t
e
r 4 L
o
o
p
s
a
n
d
A
r
r
a
y
s
print “<h3>Binary numbers</h3>\n”;

for ($i = 0; $i < count($binary); $i++){
print “$i: $binary[$i]<br>\n”;
} // end for loop
Note that my loop sentry goes from 0 to 1 less than the number of elements in
the array. If you have four elements in an array and the array begins with
0, the
largest index is
3. This is a standard way of looping through an array.
Since it is so common to step through arrays, PHP provides another kind of loop
that makes this even easier. You get a chance to see that looping structure in
chapter 5, “Better Arrays and String Handling.” For now, understand how an
ordinary for loop is used with an array.
Improving This Old Man
with Arrays and Loops
The basicArray.php program shows how to build arrays, but it doesn’t illustrate
the power of arrays and loops working together. To see how these features can
help you, revisit an old friend from chapter 3, “Controlling Your Code with
Conditions and Functions.” The version of the
This Old Man program featured in
Figure 4.8 looks a lot like it did in chapter 3, but the code has quite a bit more
compact.
TRICK
FIGURE 4.8
The Fancy Old
Man program uses
a more compact
structure than
This Old Man.
The improvements in this version are only apparent when you look under the hood:
<html>

<head>
<title>
Fancy Old Man
</title>
</head>
<body>
<h1>This Old Man with Arrays</h1>
<pre>
<?
$place = array(
“”,
“on my thumb”,
“on my shoe”,
“on my knee”,
“on a door”);
//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
?>
</pre>
</body>
</html>
This improved version takes advantage of the fact that the only things that

change from verse to verse is the verse number and the place where the old man
plays paddy-whack (whatever that means). Organizing the places into an array
greatly simplify writing out the song lyrics.
112
P
H
P
5
/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e

A
b
s
o
l
u
t
e
B
e
g
i
n
n
e
r

×