Outline
26.1
26.2
26.3
26.4
26.5
26.6
26.7
26.8
26.9
26.10
26.11
Introduction
PHP
String Processing and Regular Expressions
Viewing Client/Server Environment Variables
Form Processing and Business Logic
Verifying a Username and Password
Connecting to a Database
Cookies
Dynamic Content in PHP
Operator Precedence
Web Resources
Objectives
In this chapter, you will learn:
26.1 Introduction
PHP
Platform independent
26.2 PHP
Basic application
Scripting delimiters
<? php ?>
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
4
6
7
<?php
8
9
Scripting delimiters
first.php
(1 of 1)
$name = "LunaTic";
// declaration
?>
10
11 <html xmlns = "http://www.w3.org/1999/xhtml">
12
13
14
<head>
comment
</head>
15
16
17
18
<strong>
19
20
21
22
</strong>
23
</p>
24
</body>
25 </html>
26.2 PHP
Fig. 26.1
26.2 PHP
Variables
Can have different types at different times
Variable names inside strings replaced by their value
Type conversions
settype function
Type casting
Concatenation operator
. (period)
Combine strings
26.2 PHP
Description
int, integer Whole numbers (i.e., numbers without a decimal point).
float, double Real numbers (i.e., numbers containing a decimal point).
string
Text enclosed in either single ('') or double ("") quotes.
bool, Boolean True or false.
array
Group of elements of the same type.
object
Group of associated data and methods.
Resource
An external data source.
NULL
No value.
Fig. 26.2 PHP data types.
Data type
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
data.php
(1 of 3)
6
7
8
9
10
11
12
<body>
13
14
<?php
15
16
17
18
$testDouble = 79.2;
19
$testInteger = 12;
20
?>
21
22
23
24
25
Outline
data.php
of 3)
Print each variables (2
value
26
27
<br />
28
29
<?php
30
31
32
33
print( "$testString" );
34
35
36
print( "$testString" );
37
38
39
40
41
function
settype
print( "Converting back toCall
a string
results
in
convert
the data type
$testString <br /><br
/>"
); settype
Call function
to
42
43
$data = "98.6
to
of
variable
to a
convert
the data$testString
type of
double.
degrees";
variable
$testString to an
integer.
Convert variable $testString
back to a string
1
0
44
45
46
// different type
47
48
49
50
51
?>
52
</body>
53 </html>
Outline
data.php
(3 of 3)
Use type casting to cast variable
$data to different types
1
1
12
26.2 PHP
Fig. 26.3
Type conversion.
13
26.2 PHP
Arithmetic operators
Assignment operators
Syntactical shortcuts
Before being assigned values, variables have value undef
Constants
Named values
define function
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
operators.php
(1 of 3)
6
7
8
9
10
11
12
13
<body>
<?php
14
$a = 5;
15
16
17
18
define( "VALUE", 5 );
19
20
21
$a = $a + VALUE;
22
23
is $a <br />" );
24
1
4
25
// multiply variable $a by 2
26
$a *= 2;
27
28
operators.php
50
Print if variable $a isTest
lesswhether
than 50.variable $a is less(2than
of 3)
29
30
if ( $a < 50 )
31
Outline
32
33
// add 40 to variable $a
34
$a += 40;
35
36
37
38
if ( $a < 51 )
39
40
41
42
43
44
45
46
47
inclusive<br />" );
else
print( "Variable a is now greater than 100
<br />" );
48
1
5
49
50
51
Outline
52
53
54
55
56
57
Print anAdd
uninitialized
variable
$nothing
).
constant VALUE
to (an
uninitialized
an integer
variable.
58
// add a string to
59
60
$a += $str;
61
62
63
64
operators.php
(3 of 3)
<br />" );
?>
</body>
65 </html>
1
6
17
26.2 PHP
Fig. 26.4
18
26.2 PHP
Keywords
Reserved for language features
ifelseifelse
Arrays
Group of related data
Elements
array function
19
26.2 PHP
Arrays, cont.
Built-in iterators
Maintain pointer to element currently referenced
reset
key
next
foreach loops
20
26.2 PHP
PHP keywords
and
break
case
class
continue
default
do
else
elseif
extends
false
for
foreach
function
global
if
include
list
new
not
or
require
return
static
switch
this
true
var
virtual
xor
while
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
arrays.php
(1 of 3)
-->
6
7
8
9
10
11
12
13
<body>
<?php
14
15
16
17
<br />" );
18
$first[ 0 ] = "zero";
19
$first[ 1 ] = Assign
"one";
22
23
24
20
21
25
2
1
26
31
Outline
Call
function
array
to
create
an
array
that
contains
</strong><br />" );
the arguments passed to it. Store the array in variable
$second.
arrays.php
// call function array to create array second
(2 of 3)
$second = array( "zero", "one", "two", "three" );
32
27
28
29
30
33
34
35
36
37
38
39
40
41
$third[ "GalAnt" ] =
42
43
44
45
46
next( $third ) )
47
48
2
2
49
50
Outline
</strong><br />" );
51
52
53
// string indices
54
$fourth = array(
55
"January"
=> "first",
56
"March"
=> "third",
"April"
57
"May"
=>
58
"July"
=>
59
"September" =>
60
"November"
61
);
=>
arrays.php
(3 of 3)
=> "fourth",
is used
in function array to assign each
"fifth", Operator
"June" =>=>
"sixth",
a string
index. The value to the left of the
"seventh",element
"August"
=> "eighth",
is the
index, and the value to the right is
"ninth", operator
"October"
=> array
"tenth",
the elements=>value.
"eleventh","December"
"twelfth"
62
63
64
65
66
?>
67
</body>
68 </html>
2
3
24
26.2 PHP
Fig. 26.6
Array manipulation.
Relational operators
25
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
compare.php
(1 of 2)
-->
6
7
8
9
10
11
12
13
<body>
<?php
14
15
16
17
18
// iterate through
19
for ( $i = 0; $i <
20
21
22
// to string "banana"
23
24
2
6
25
26
Outline
print( $fruits[ $i ].
27
28
else
29
30
31
32
// to string "apple"
33
34
35
36
37
38
39
40
41
42
}
?>
</body>
43 </html>
2
7
Fig. 26.7
28
Brackets [ ]
29
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
expression.php
(1 of 3)
7
8
9
10
11
12
13
<body>
<?php
14
$search = "Now is
15
16
17
18
// in variable search
19
20
21
3
0
22
23
// the string
24
25
26
27
28
//
29
30
31
Outline
32
33
34
35
$match ) )
36
37
38
The expression
the expressions
parentheses,[[:<:]]
[a-zA-Z]*ow
The specialinside
bracket
and ,
matches
any
word
ending
in ow. The
quantifier
Placing
a pattern
in the
parentheses
stores
the matched
[[:>:]]
match
beginning
and
end
of *a word,
with 't' found: ");
matches
the
preceding
pattern
0 or more
times.
string
in the
array that
is specified
in the
third argument
respectively.
to function ereg.
39
40
41
42
43
$search, $match ) ) {
44
45
3
1
46
47
48
49
Outline
50
51
52
?>
53
</body>
54 </html>
expression.php
After printing a match of a word beginning with t, function
(3 string.
of 3)
ereg_replace is called to remove the word from the
This is necessary be because to find multiple instances of a
given pattern, the first matched instance must first be removed.
Function ereg_replace takes three arguments: the pattern to
match, a string to replace the matched string and the string to
search.
3
2
Fig. 26.8
33
Matches
{n}
{m,n}
{n,}
+
*
?
Exactly n times.
Between m and n times inclusive.
n or more times.
One or more times (same as {1,}).
Zero or more times (same as {0,}).
Zero or one time (same as {0,1}).
Fig. 26.9
34
Description
alnum
alpha
digit
space
lower
upper
Fig. 26.10
35
$_ENV
36
Variable name
Description
Data about the currently running server.
$_SERVER
Data about the clients environment.
$_ENV
Data posted to the server by the get method.
$_GET
Data posted to the server by the post method.
$_POST
Data contained in cookies on the clients computer.
$_COOKIE
Array containing all global variables.
$GLOBALS
Fig. 26.11
Some useful global arrays.
37
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
env.php
(1 of 1)
7
8
9
10
11
12
13
<body>
<table border = "0" cellpadding = "2" cellspacing = "0"
14
15
width = "100%">
<?php
16
17
18
19
20
21
22
23
?>
24
</table>
25
</body>
26 </html>
3
8
Fig. 26.12
39
40
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
form.html
(1 of 4)
7
8
9
10
11
12
<body>
13
14
<h1>This is
15
Please fill
16
17
18
19
20
21
22
23
4
1
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
4
2
48
49
50
51
52
53
54
</select>
55
Outline
form.html
(3 of 4)
56
57
58
59
60
61
62
63
64
65
Windows XP
66
67
68
69
Windows 2000
70
71
72
73
4
3
74
75
Outline
76
Linux
77
78
form.html
(4 of 4)
79
Other<br />
80
81
82
83
84
85
</body>
86 </html>
4
4
45
Fig. 26.13
46
Ending a script
die function
Remember to close all HTML tags
2003 Prentice Hall, Inc. All rights reserved.
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
form.php
(1 of 4)
7
8
9
10
11
12
13
14
15
<?php
extract( $_POST
16
17
18
19
if ( !ereg( "^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$",
20
21
$phone ) ){
4
7
22
23
font-size: 2em\">
24
25
26
<strong>(555)555-5555</strong><br />
27
28
29
30
Thank You.</span></p></body></html>" );
Outline
form.php
(2 of 4)
31
32
33
34
}
?>
35
36
37
38
39
40
<p>Hi
<span style = "color: blue">
<strong>
<?php print( "$fname" ); ?>
</strong>
41
</span>.
42
43
4
8
44
45
46
<strong>
47
48
form.php
(3 of 4)
</strong>
49
</span>
50
mailing list.
51
</p>
52
53
Outline
54
55
56
57
58
59
60
61
</tr>
62
63
64
<tr>
<?php
65
4
9
66
67
68
<td>$email</td>
69
<td>$phone</td>
70
<td>$os</td>" );
71
?>
72
</tr>
73
</table>
Outline
form.php
(4 of 4)
74
75
76
77
78
79
80
</div>
</body>
81 </html>
5
0
51
Fig. 26.14
52
fclose function
53
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
password.html
(1 of 4)
7
8
9
10
11
12
13
14
15
16
17
18
19
<br />
20
21
font-weight: bold">
22
23
24
</span>
</p>
25
5
4
26
27
28
<br />
29
30
Outline
password.html
(2 of 4)
31
32
33
34
35
<tr>
<td colspan = "3">
36
37
38
<strong>Username:</strong>
</td>
</tr>
39
40
41
<tr>
<td colspan = "3">
42
43
44
45
46
5
5
47
48
<tr>
49
50
51
Outline
password.html
(3 of 4)
</tr>
52
53
54
<tr>
<td colspan = "3">
55
56
57
58
59
<br/></td>
</tr>
60
61
62
63
<tr>
<td colspan = "1">
<input type = "submit" name = "Enter"
64
65
66
</td>
67
68
69
70
71
</td>
5
6
72
</tr>
73
</table>
74
75
</form>
Outline
</body>
password.html
(4 of 4)
76 </html>
5
7
58
Fig. 26.15
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
password.php
(1 of 7)
7
8
9
10
11
12
13
if ( !$USERNAME || !$PASSWORD ) {
14
fieldsBlank();
15
die();
16
17
18
19
if ( isset( $NewUser ) ) {
20
21
22
23
"a" ) ) ) {
24
5
9
25
26
27
print( "<title>Error</title></head><body>
28
29
</body></html>" );
30
die();
31
Outline
password.php
Print an error message and terminate script
(2 ofexecution
7)
if the file cannot be opened.
32
33
34
35
36
userAdded( $USERNAME );
37
38
else {
39
40
// if a new user
41
// for reading
42
43
"r" ) ) ) {
44
print( "<title>Error</title></head>
45
46
</body></html>" );
47
48
die();
}
49
6
0
50
$userVerified = 0;
51
52
53
54
loop, variable
// and password Function$userVerified
fgets reads a line
from
the
is set
to 0
. text file.
while ( !feof( $file
) && !$userVerified
) {
The result
is assigned to variable
$line.
55
56
57
58
59
60
61
62
63
64
password.php
(3 of 7)
65
// verify username
66
if ( $USERNAME == $field[ 0 ] ) {
67
Outline
$userVerified = 1;
68
69
If function
// call function checkPassword to verify
70
// users password
71
permission
if ( checkPassword( $PASSWORD, $field
)
72
== true )
73
accessGranted( $USERNAME );
74
75
else
wrongPassword();
6
1
76
77
78
79
// close text
80
fclose( $file
81
Outline
82
83
84
if ( !$userVerified )
85
accessDenied();
86
87
88
89
90
91
if ( $userpassword == $filedata[ 1 ] )
92
return true;
93
else
94
95
return false;
}
96
6
2
97
98
99
100
101
102
font-size: 1em;
103
104
105
106
Outline
password.php
(5 of 7)
107
108
109
110
111
112
113
114
115
116
117
118
119
6
3
120
121
function wrongPassword()
122
Outline
125
126
127
128
been denied.</strong>" );
123
124
129
130
131
132
function accessDenied()
133
134
135
136
137
<strong>
138
139
<br /></strong>" );
140
141
6
4
142
143
144
145
146
147
148
149
<strong>
150
151
<br /></strong>" );
152
153
?>
154
</body>
155 </html>
6
5
66
Fig. 26.16
account1,password1
account2,password2
account3,password3
account4,password4
account5,password5
account6,password6
account7,password7
account8,password8
account9,password9
Outline
password.txt
(1 of 1)
10 account10,password10
6
7
68
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
data.html
(1 of 2)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-->
6
9
22
23
24
<option>ID</option>
25
<option>Title</option>
26
<option>Category</option>
27
<option>ISBN</option>
28
Outline
</select>
29
</p>
30
31
32
33
34
35
</form>
</body>
36 </html>
7
0
71
Fig. 26.18
72
mysql_connect
mysql_select_db
mysql_query
mysql_error
mysql_fetch_row
mysql_close
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
database.php
(1 of 3)
-->
7
8
9
10
11
12
13
14
15
<?php
16
17
extract( $_POST );
18
19
20
21
22
// Connect to MySQL
23
24
"httpd", "" ) ) )
25
7
3
26
27
28
if
29
30
31
//
32
if
33
34
Function) mysql_query
returns an object
( !mysql_select_db( "Products", $database
)
containing
the result set of the query, which
die( "Could not open Products database"
);
we assign to variable $result.
database.php
query Products database
(2 of 3)
( !( $result
= mysql_query(
$query, $database
) specify
) ) {
Function
mysql_select_db
is called to
the
print( database
"Could not
query! <br />" );
toexecute
be queried.
die( mysql_error() );
35
36
Outline
}
?>
37
38
39
Search Results</h3>
40
41
42
43
44
45
46
47
48
49
50
<?php
7
4
51
52
print( "<tr>" );
Outline
53
54
55
database.php
(3 of 3)
print( "</tr>" );
The foreach loop iterates through the
}
array containing the elements of each row
and prints out each element in an
mysql_close( $databaseThe
); total number of results are printed to the
individual table cell.
client.
print( "<td>$value</td>" );
56
57
58
59
60
61
?>
62
63
</table>
64
65
66
67
68
69
70
71
72
</a>
</h5>
73
74
</body>
75 </html>
7
5
76
Fig. 26.19
77
26.8 Cookies
Cookies
setcookie function
Name
Value
Expiration date
2003 Prentice Hall, Inc. All rights reserved.
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
cookies.html
(1 of 2)
-->
6
7
8
9
10
11
12
13
14
15
16
7
8
17
18
Outline
19
<strong>Name:</strong><br />
20
cookies.html
(2 of 2)
21
22
<strong>Height:</strong><br />
23
24
25
26
27
28
29
30
31
32
</form>
</body>
33 </html>
7
9
80
26.8 Cookies
Fig. 26.20
<?php
Outline
4
5
extract( $_POST );
10
cookies.php
(1 of 2)
11 ?>
12
13
14
15
<head>
<title>Cookie Saved</title>
</head>
20
21
22
23
8
1
24
25
26
27
28
29
30
31
32
33
34
</span><br />
35
36
37
38 </html>
Hyperlink to readCookies.php.
8
2
83
26.8 Cookies
84
26.8 Cookies
Reading cookies
$_COOKIE environment variable
Array
foreach loop to access each element
85
26.8 Cookies
Cookie storage
Internet Explorer
86
26.8 Cookies
Fig. 26.22 Cookies directory before a cookie is written.
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
readCookies.php
(1 of 2)
<head><title>Read Cookies</title></head>
9
10
11
12
13
<p>
<strong>
14
15
computer.
16
17
</strong>
</p>
18
8
7
19
The foreach
loop
iterates
through
the $_COOKIE
PHP
creates
array
$_COOKIE
which contains all Outline
array and prints
thevalues
name and
valuebyoftheir
eachnames.
cookie in
cookie
indexed
// iterate through array $_COOKIE and print
an XHTML table.
readCookies.php
// name and value of each cookie
(2 of 2)
foreach ( $_COOKIE as $key => $value )
20
<?php
21
22
23
24
25
print( "<tr>
26
<td bgcolor=\"#F0E68C\">$key</td>
27
<td bgcolor=\"#FFA500\">$value</td>
28
</tr>" );
29
?>
30
31
32
</table>
</body>
33 </html>
8
8
89
26.8 Cookies
90
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
dynamicForm.php
(1 of 9)
7
8
9
10 </head>
11
12 <body>
13
<?php
14
extract ( $_POST );
15
$iserror = false;
16
17
18
19
20
21
22
9
1
23
24
25
"Windows 2000",
26
"Windows 98",
27
"Linux",
28
"Other");
Outline
dynamicForm.php
(2 of 9)
29
30
// array of name and alt values for the text input fields
31
32
33
34
"phone" =>
35
36
if ( isset ( $submit ) ) {
37
if ( $fname == "" ) {
38
39
$iserror = true;
40
41
42
if ( $lname == "" ) {
43
44
$iserror = true;
45
46
9
2
47
if ( $email == "" ) {
48
49
$iserror = true;
50
Outline
dynamicForm.php
(3 of 9)
51
52
53
54
$iserror = true;
55
56
57
58
if ( !$iserror ) {
59
60
61
62
63
64
65
// Connect to MySQL
66
67
"httpd", "" ) ) )
68
69
70
71
72
9
3
73
74
75
76
77
die( mysql_error() );
78
Outline
dynamicForm.php
(4 of 9)
79
80
print( "<p>Hi
81
82
<strong>$fname</strong></span>.
83
84
85
86
87
<strong>$book</strong></span>
88
mailing list.
89
</p>
90
91
92
93
94
<tr>
95
96
97
9
4
98
99
</tr>
100
<tr>
Outline
101
102
103
<td>$fname $lname</td>
104
<td>$email</td>
105
<td>$phone</td>
106
<td>$os</td>
107
</tr></table>
dynamicForm.php
(5 of 9)
108
109
110
111
112
113
114
115
116
</div></body></html>" );
117
die();
118
119
}
}
120
121
122
9
5
123
124
125
126
127
Outline
if ( $iserror ) {
Fields with * need to be filled in properly.</span>" );
dynamicForm.php
(6 of 9)
128
129
130
131
132
133
134
</span>
135
136
137
138
139
140
141
142
143
144
145
146
147
148
9
6
149
150
Outline
151
152
153
if ( $formerrors[ "phoneerror" ] )
dynamicForm.php
(7 of 9)
154
155
156
157
158
159
160
161
162
163
</span><br />
164
165
166
167
168
169
170
171
172
if ( ( $currbook == $book ) )
print( " selected = 'true'" );
173
9
7
174
175
print( ">$currbook</option>" );
Outline
176
177
178
179
180
181
<br /></span>
dynamicForm.php
(8 of 9)
182
183
184
185
$counter = 0;
186
187
188
189
value = '$currsystem'" );
190
191
192
193
194
195
196
197
$counter++;
198
199
9
8
200
201
<br />
202
203
</form></body></html>" );
204
Outline
dynamicForm.php
(9 of 9)
?>
9
9
100
101
1
2
Outline
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
-->
-->
formDatabase.php
(1 of 3)
7
8
9
10
11
12
13
14
15
<?php
16
17
18
19
20
21
22
// Connect to MySQL
23
24
"httpd", "" ) ) )
25
1
0
2
26
27
28
29
Outline
formDatabase.php
(2 of 3)
30
31
32
33
34
die( mysql_error() );
35
36
}
?>
37
38
39
40
41
42
43
44
<tr>
45
<td>ID</td>
46
<td>Last Name</td>
47
<td>First Name</td>
48
<td>E-mail Address</td>
49
<td>Phone Number</td>
50
<td>Book</td>
1
0
3
51
<td>Operating System</td>
52
</tr>
53
<?php
54
55
56
for ( $counter = 0;
57
58
$counter++ ){
59
60
// build table to
61
print( "<tr>" );
62
Outline
formDatabase.php
(3 of 3)
63
64
print( "<td>$value</td>" );
65
66
print( "</tr>" );
67
68
69
mysql_close( $database );
70
?>
71
72
</table>
73
74
</body>
75 </html>
1
0
4
105
106
Operator
new
[]
~
!
++
-@
*
/
%
+
.
<<
>>
<
>
<=
>=
==
!=
===
!==
Fig. 26.27
Type
Associativity
constructor
subscript
bitwise not
not
increment
decrement
unary negative
error control
multiplication
division
modulus
addition
subtraction
concatenation
bitwise shift left
bitwise shift right
less than
greater than
less than or equal
greater than or equal
equal
not equal
identical
not identical
none
right to left
right to left
left to right
left to right
left to right
none
none
107
and
xor
or
,
Fig. 26.27
Type
Associativity
bitwise AND
bitwise XOR
bitwise OR
logical AND
logical OR
assignment
addition assignment
subtraction assignment
multiplication assignment
division assignment
bitwise AND assignment
bitwise OR assignment
bitwise exclusive OR assignment
concatenation assignment
bitwise shift left assignment
bitwise shift right assignment
logical AND
exclusive OR
logical OR
list
left to right
left to right
left to right
left to right
left to right
left to right
left to right
left to right
left to right
left to right