
- •Initializing with Constructor Functions . . . . .
- •Into a Web page as a separate section. Although JavaScript code can
- •Is that standard php script delimiters are guaranteed to be available
- •In the block. Any text or lines between the opening /* characters and
- •2.7541 Are not integers; they are floating-point numbers. A floating-
- •Value 300
- •Is a value of 2.5, because 6 goes into 15 exactly 2.5 times. But if you
- •IsEven.Php.
- •Ing example,
- •Ing curly brace is on its own line following the function statements.
- •In php 3 and earlier, it was necessary to put a function definition
- •Is called an iteration. When the conditional expression evaluates
- •Including Files
- •13. Close your Web browser window.
- •Including Files
- •In php, you can also use two operators to combine strings. The first
- •Xhtml source code gen-
- •Input. Php provides several functions for manipulating the case of a
- •Is uppercase. If you need the reverse of ucfirst(), the lcfirst()
- •In some situations, you will need to find and extract characters and
- •Information Interchange, or ascii, which are numeric represen-
- •In comparison, the following preg_match() function returns a value
- •In the pattern is optional. The following code demonstrates how to
- •Values; any strings you validate against a regular expression must
- •Value of 1 because the top-level domain contains a valid value of .Com.
- •Is submitted using the “post” method, the form data is embedded in
- •Validating String Data
- •Xhtml tags or character entities. The message field is a text string
- •Value of the header element. For example:
- •Xhtml code within a php script section.
- •Is typically the person who created the resource. Otherwise, the net-
- •If even a single character of the Web page is sent prior to sending
- •Variables to the file_put_contents() function.
- •Xhtml hyperlink. To download a file from outside the xhtml
- •If...Else statement to display the appropriate version of the mes-
- •Iterating Through an Array
- •Iterating Through an Array
- •In Chapter 2, you learned how to use a foreach statement to iterate
- •Iterating Through an Array
- •Iterating Through an Array
- •In comparison, the following code declares and initializes
- •If ((!file_exists("MessageBoard/messages.Txt"))
- •Values from the array to create a thumbnail gallery of images in which
- •Introduction to Databases
- •Including php, allow you to create Web pages that can read and write
- •Introduction to Databases
- •Information that can be organized into ordered sets of data, and
- •Information. Each recipe in a recipe database, for instance, is a single
- •Introduction to Databases
- •Index, which identifies records in a database to make retrievals and
- •In a single table. However, you might want to break the information
- •Into multiple tables to better organize it into logical sets. Another
- •Information in one of the tables confidential and accessible only by
- •Is the employee information table from Figure 7-1. The related table
- •Is a payroll table that contains confidential salary and compensation
- •Information. Notice that each table contains an identical number of
- •Introduction to Databases
- •Introduction to Databases
- •In a junction
- •Introduction to Databases
- •In a relational format is called a relational database management
- •Is a standard data manipulation language among many dbmSs.
- •Into the query area at the top of the screen or by dragging tables and
- •It is important to understand that even though many dbmSs sup-
- •Introduction to Databases
- •If you ever
- •Is. In comparison, the bigint data type stores integer values between
- •5 Rows in set (0.00 sec)
- •Int);[enter ]
- •Important, these two tabs can cause you to lose all of the data in the
- •Internet Explorer to export the table, click the Save button in the File
- •Ifies the table being changed and the change to make.
- •It easier for you to write php code that can be used with a variety of
- •Information about queries that match one of the following formats:
- •Various types of actions, depending on the type of query.
- •Include fields for the date and time of the flight, flight number, and
- •In the ChineseZodiac folder and upload the file to the server. Open
- •Including white space,
- •Information on a Web server. When you start a new session, the
- •Introduction to Object-Oriented Programming
- •Introduction to Object-Oriented
- •Variables associated with an object are called properties or attributes.
- •In the Loan object example, a function that calculates the number of
- •Introduction to Object-Oriented Programming
- •Introduction to Object-Oriented Programming
- •Include instances of objects inherit the object’s functionality.
- •In this chapter, you will create the Web site for an online order form
- •In an online store application. The application includes information
- •Ity of building a working online store. Online store classes are very
- •Information and products. The OnlineStore class requires that store
- •Information is stored in a table containing six fields: storeId, name,
- •Information. Instead, the class simply uses session iDs to keep track
- •Variable and function as necessary, without bothering with all this
- •In a class
- •Is developed. Imagine what would happen if Microsoft distributed
- •Ing class is invalid because it does not include an access specifier:
- •If they will not be supported by future xhtml versions or are not
- •Xhtml standards. To review the guide of current w3c css specifi-
- •Information to remind yourself or others of what the code is doing. A
- •Xhtml document to the external style sheet. This link informa-
- •If you select Apache from the WampServer menu and select Service
- •Ing code uses the number_format() function to add comma separa-
- •In data that a user submits to a php script.
- •Value of “On” and the display_startup_errors directive is assigned
- •Instead. By looking at the source code, you could see that the value of
- •Ing engine can even help locate logic errors.
- •In Chapter 8, along with the equivalent mssql_* functions, where
- •Inline styles, 632
- •Xhtml, 620–635 (continued)
Xhtml hyperlink. To download a file from outside the xhtml
directory structure, use the header() function to identify the file
and the readfile() function to send the file contents to the Web
browser.
• The
file_put_contents() function writes or appends a text stringto a file. The file_get_contents() function reads a file into a text
string. The file() function reads a file into an indexed array.
• The stream is used for accessing a resource from which you can
read and to which you can write. The input stream reads data from
a resource such as a file, whereas the output stream writes data to a
resource (again, such as a file).
• The
fopen() function opens a stream to a text file.• A file pointer is a special type of variable that refers to the cur-
rently selected line or character in a file.
• When you finish working with a file stream, you use the
fclose()function to ensure that the file doesn’t keep taking up space in your
computer’s memory.
• The
fwrite() function incrementally writes data to a text file.• The
flock() function prevents multiple users from modifying a filesimultaneously.
284
Comprehension
Check
•
The
the file.
• PHP includes various functions, such as the
fread() andfgets() functions, that allow you to use the file pointer to
iteratively read a text file.
• The
copy() function copies a file. The rename() function renamesor moves a file or directory.
• The
unlink() function deletes files, and the rmdir() functiondeletes directories.
• PHP does not contain a separate command for moving files.
Instead, you can rename the file with the rename() function and
specify a new directory where you want to store the renamed file.
Or, you must copy the file with the copy() function, and then
delete the original file with the unlink() function.
285
Comprehension Check
1.
Which of the following escape sequences is used on
Macintosh platforms? (Select all that apply.)
a. \n
b. \r
c. \n\r
d. \r\n
2.
Which of the following functions sorts directory entries?
a. scandir()
b. readdir()
c. opendir()
d. sortdir()
3.
Explain when you should use file and directory status func-
tions such as file_exists() and is_dir().
What is the value of the enctype attribute for a Web form
that uploads a file?
What is the name of the autoglobal array that contains
uploaded file information?
4.
5.
CHAPTER
5
Working
with Files and Directories
6.
Which
of the following constants can you use with the
file_put_contents()
function
to append data to the end of a
file?
a.
INCLUDE_FILE
286
b.
FILE_USE_INCLUDE_PATH
c.
APPEND
d.
FILE_APPEND
7.
Which
of the following functions reads the contents of a file
into
a string?
a.
file()
b.
file_get_contents()
c.
fread()
d.
readfile()
8.
The
file()
function
automatically recognizes whether the
lines
in a text file end in \n,
\r,
or \n\r. True
or False?
Which
of the following allows you to read data from a
resource
such as a file?
a.
an input stream
b.
an output stream
c.
a pointer
d.
a reference
10.
Which of the following best describes the "w+"
method
argument?
a.
creates and opens the specified file for reading and writ-
ing;
returns
b. opens the specified file for reading and writing and places
the file pointer at the end of the file; attempts to create the
file if it doesn’t exist
c. opens the specified file for writing only and deletes any
existing content in the file; attempts to create the file if it
doesn’t exist
d. opens the specified file for reading and writing and deletes
any existing content in the file; attempts to create the file
if it doesn’t exist
9.

Comprehension
Check
11.
Ais a special type of variable that refers to
the
currently selected character in a file.
a. character pointer
b. line pointer
c. file pointer
d. directory pointer
12. Explain why you should call the fclose() function when you
are finished working with a file.
13. You must open and close a file stream when you use the
file_put_contents() function. True or False?
14. What is the correct syntax for using the fwrite() function to
write a value of “Forestville Foods\n” to a file handle named
$SalesProspects?
a. $SalesProspects = fwrite("Forestville Foods\n");
b. fwrite($SalesProspects, "Forestville Foods\n");
c. fwrite("Forestville Foods\n", $SalesProspects);
d. fwrite("$SalesProspects, Forestville Foods\n");
15. Explain why you should lock files before writing data to
them.
16. Which of the following operational constants can you use
with the
flock() function? (Choose all that apply.)a. LOCK_EX
b. LOCK_NH
c. LOCK_SH
d. LOCK_UH
17. Which of the following functions can you use to iterate
through a text file? (Choose all that apply.)
a. stream_get_line()
b. fgets()
c. fread()
d. readfile()
287

CHAPTER
5
Working
with Files and Directories
18.
Which of the following functions returns a value of
TRUE
when
a file pointer reaches the end of a file?
a.
is_end()
b.
end()
288
c.
eof()
d.
feof()
19.
Which of the following statements creates a directory named
“students”
at the same level as the current directory?
a. mkdir("/students");
b. mkdir("students");
c. mkdir("/students/");
d. mkdir("../students");
20. Explain the two ways in which you can move a file with PHP.
Reinforcement Exercises
Exercise 5-1
In this project, you will create a hit counter script that keeps track
of the number of hits a Web page receives. Ensure that the Projects
directory has read and write permissions for everyone.
1.
Create a new document in your text editor and type the
<!DOCTYPE> declaration, <html> element, document head, and
<body> element. Use the strict DTD and “Hit Counter” as the
content of the <title> element.
Add the following script section to the document body:
<?php
?>
2.
3.
Add the following statement to the script section to declare a
variable named $CounterFile that contains the name of the
file where the hits will be stored:
$CounterFile = "hitcount.txt";
4.
Add the following if statement to the end of the script sec-
tion. The if statement determines whether the hitcount.txt

Reinforcement
Exercises
file
already exists. If it does, the file_get_contents()
func-
tion
retrieves the value from the file and increments it by 1.
if
(file_exists($CounterFile)) {
$Hits
= file_get_contents($CounterFile);
++$Hits;
}
289
5.
Add the following else statement to the end of the script
section. The else statement contains a single statement that
assigns a value of 1 to the $Hits variable in the event that the
hitcount.txt file has not yet been created.
else
$Hits = 1;
6.
Finally, add the following statements to the end of the script
section. The echo statement displays the number of hits and
the if statement updates the value in the hitcount.txt file.
Remember that the file_put_contents() function opens the
file if it already exists or creates the file if it doesn’t exist.
echo "<h1>There have been $Hits hits to this page.
</h1>\n";
if (file_put_contents($CounterFile, $Hits))
echo "<p>The counter file has been updated.
</p>\n";
7.
Save the document as HitCounter.php in the Projects direc-
tory for Chapter 5.
Open HitCounter.php in your Web browser by entering
the following URL: http://<yourserver>/PHP_Projects/
Chapter.05/Projects/HitCounter.php. The first time you open
the Web page, you should see a hit count of 1. Reload the Web
page a few times to see if the count increases.
Close your Web browser window.
8.
9.
Exercise 5-2
In this project, you will create a Web page that allows visitors to your
site to sign a guest book that is saved to a text file. Ensure that the
Projects directory has read and write permissions for everyone.
1.
Create a new document in your text editor and type the
<!DOCTYPE> declaration, <html> element, document head, and
<body> element. Use the strict DTD and “Guest Book” as the
content of the <title> element.
CHAPTER
5
Working
with Files and Directories
2.
Add
the following text and elements to the document body:
<h2>Enter
your name to sign our guest book</h2>
<form
method="POST" action="SignGuestBook.php">
<p>First
Name <input type="text" name="first_name"
/></p>
<p>Last
Name <input type="text" name="last_name"
/></p>
<p><input
type="submit" value="Submit" /></p>
</form>
<p><a
href="ShowGuestBook.php">Show Guest Book
</a></p>
290
3.
Save
the document as GuestBook.html in the Projects direc-
tory
for Chapter 5.
Create
a new document in your text editor and type the
<!DOCTYPE>
declaration,
<html>
element,
document head, and
<body>
element.
Use the strict DTD and “Sign Guest Book” as
the
content of the <title>
element.
Add
the following script section to the document body:
<?php
?>
4.
5.
6.
Add
the following if
statement
to the script section to check
whether
the user filled in the first name and last name fields:
if
(empty($_POST['first_name']) || empty($_
POST['last_name']))
echo
"<p>You must enter your first and last
name. Click your browser's Back button to
return to the Guest Book.</p>\n";
7.
Add the following else clause to the end of the script section.
The statements in the else clause use the fwrite() function
to add visitor names to a text file named guestbook.txt.
else {
$FirstName = addslashes($_POST['first_name']);
$LastName = addslashes($_POST['last_name']);
$GuestBook = fopen("guestbook.txt", "ab");
if (is_writeable("guestbook.txt")) {
if (fwrite($GuestBook, $LastName . ", " .
$FirstName . "\n"))
echo "<p>Thank you for signing our
guest book!</p>\n";
else
echo "<p>Cannot add your name to the
guest book.</p>\n";
}

Reinforcement
Exercises
else
echo
"<p>Cannot write to the file.</p>\n";
fclose($GuestBook);
}
8.
Save the document as SignGuestBook.php in the Projects
directory for Chapter 5.
Create a document named ShowGuestBook.php that dis-
plays the names of visitors who have signed the guest book.
Use the readfile() function to display the contents of the
guestbook.txt file. Note that you will need to use the <pre>
element for Web browsers to recognize the line breaks.
291
9.
10. Open GuestBook.html in your Web browser by enter-
ing the following URL: http://<yourserver>/PHP_Projects/
Chapter.05/Projects/GuestBook.html. Test the form to see if
you can write data to and read data from the guestbook.txt
file.
11. Close your Web browser window.
Exercise 5-3
Create a document with a form that registers bowlers for a bowl-
ing tournament. Use a single text file that saves information for each
bowler on a separate line. Include the bowler’s name, age, and aver-
age, separated by commas. Ensure that the Projects directory has read
and write permissions for everyone.
Exercise 5-4
Create a Web page to be used for storing software development bug
reports in text files. Include fields such as product name and ver-
sion, type of hardware, operating system, frequency of occurrence,
and proposed solutions. Include links on the main page that allow
you to create a new bug report and update an existing bug report.
Ensure that the Projects directory has read and write permissions for
everyone.
Exercise 5-5
Create a Web form for uploading pictures for a high school reunion.
The form should have text input fields for the person’s name and
