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

PHP 5/MySQL Programming- P11 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 (176.95 KB, 5 trang )

Using Variables for
More-Complex Pages
While the Hello Jacob program was interesting, there is no real advantage to
using a variable. Check out another use for variables.
Building the Row Your Boat Page
Figure 2.5 shows the Row Your Boat page.
I chose this song in particular because it repeats the same verse three times. If
you look at the original code for the
rowBoat.php program, you see I used a trick
to save some typing:
<html>
<head>
<title>Row Your Boat</title>
</head>
<body>
<h1>Row Your Boat</h1>
<h3>Demonstrates use of long variables</h3>
<?
$verse = <<<HERE
28
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
FIGURE 2.5
This program
shows the words
to a popular song.
They sure
repeat a lot.
Row, Row, Row, your boat<br>
Gently Down the stream<br>
Merrily, Merrily, Merrily, Merrily<br>
Life is but a dream!<br>
<br><br>
HERE;
print “<h3>Verse 1:</h3>”;
print $verse;
print “<h3>Verse 2:</h3>”;
print $verse;
print “<h3>Verse 3:</h3>”;
print $verse;
?>
</body>
</html>
Creating Multi-Line Strings
You find yourself wanting to print several lines of HTML code at once. It can be
very tedious to use quotation marks to indicate such strings (especially because
HTML also often uses the quotation mark symbol). PHP provides a special quoting
mechanism, which is perfect for this type of situation. The following line begins
assigning a value to the
$verse variable:
$verse = <<<HERE

The <<<HERE segment indicates this is a special multi-line string that ends with
the symbol
HERE. You can use any phrase you wish, but I generally use the word
HERE because I think of the three less-than symbols as
up to.
In other words, you
can think of the following as meaning
verse gets everything up to HERE.
$verse = <<<HERE
You can also think of <<<HERE as a special quote sign, which is ended with the
value
HERE.
You can write any amount of text between <<<HERE and HERE. You can put vari-
ables inside the special text and PHP replaces the variable with its value, just like
in ordinary (quoted) strings. The ending phrase (
HERE) must be on a line by itself,
and there must be no leading spaces in front of it.
29
C
h
a
p
t
e
r 2 U
s
i
n
g
V

a
r
i
a
b
l
e
s
a
n
d
I
n
p
u
t
You might wonder why the $verse = <<<HERE line doesn’t have a semicolon
after it. Although this is one line in the editor, it begins a multi-line structure.
Technically, everything from that line to the end of the HERE; line is part of the
same logical line, even though the code takes up several lines in the editor.
Everything between <<<HERE and HERE is a string value.
The semicolon doesn’t have any special meaning inside a string. At a minimum,
you should know that a line beginning a multi-line quote doesn’t need a semi-
colon, but the line at the end of the quote does.
Once the multi-line string is built, it is very easy to use. It’s actually harder to
write the captions for the three verses than the verses themselves. The
print
statement simply places the value of the $verse variable in the appropriate spots
of the output HTML.
Working with Numeric Variables

Computers ultimately store information in on/off impulses. You can convert
these very simple data values into a number of more convenient kinds of infor-
mation. The PHP language makes most of this invisible to you, but it’s important
to know that memory handles string (
text
) differently than it does numeric
values, and that there are two main types of numeric values, integers, and floating-
point real numbers.
Making the ThreePlusFive Program
As an example of how PHP works with numbers, consider the ThreePlusFive.php
program illustrated in Figure 2.6.
All the work in the
ThreePlusFive program is done with two variables called $x
and $y. (I know, I recommended that you assign variables longer, descriptive
names, but these variables are commonly used in arithmetic problems, so these
very short variable names are okay in this instance.) The code for the program
looks like this:
<html>
<head>
<title>Three Plus Five</title>
</head>
<body>
<h1>Three Plus Five</h1>
<h3>Demonstrates use of numeric variables</h3>
TRAP
30
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
<?
$x = 3;
$y = 5;
print “$x + $y = “;
print $x + $y;
print “<br><br>”;
print “$x - $y = “;
print $x - $y;
print “<br><br>”;
print “$x * $y = “;
print $x * $y;
print “<br><br>”;
print “$x / $y = “;
print $x / $y;
print “<br><br>”;
?>
</body>
</html>
31
C
h
a

p
t
e
r 2 U
s
i
n
g
V
a
r
i
a
b
l
e
s
a
n
d
I
n
p
u
t
FIGURE 2.6
This program does
basic math on
variables
containing the

values 3 and 5.
32
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
Assigning Numeric Values
You create a numeric variable like any other variable in PHP: Simply assign a value
to a variable. Notice that numeric values do not require quotation marks. I created
variables called
$x and $y and assigned appropriate values to these variables.
Using Mathematical Operators
For each calculation, I want to print the problem as well as its solution. This line
prints out the values of the
$x and $y variables with the plus sign between them:
print “$x + $y = “;
In this particular case (since $x is 3 and $y is 5), it prints out this literal value:
3 + 5 =
Because the plus and the equals signs are inside quotation marks, they are
treated as ordinary text elements. PHP doesn’t do any calculation (such as addi-
tion or assignment) with them.
The next line does not contain any quotation marks:

print $x + $y;
It calculates the value of $x + $y and prints the result (8) to the Web page.
IN THE REAL WORLD
Numbers without any decimal point are called integers and numbers with dec-
imal values are called real numbers. Computers store these two types differ-
ently, and this distinction sometimes leads to problems. PHP does its best to
shield you from this type of issue.
For example, since the values 3 and 5 are both integers, the results of the addi-
tion, subtraction, and multiplication problems are also guaranteed to be inte-
gers. However, the quotient of two integers is often a real number. Many
languages would either refuse to solve this problem or give an incomplete result.
They might say that 3 / 5 = 0 rather than 0.6. PHP tries to convert things to
the appropriate type whenever possible, and it usually does a pretty good job.
You sometimes need to control this behavior, however. The setType() function
lets you force a particular variable into a particular type. You can look up the
details in the online Help for PHP (included in the CD that accompanies this book).

×