
- •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)
When working with strings, you often cannot guarantee that they
will be in the correct case. This is especially true when dealing with
strings from external sources, such as database queries or user form
Input. Php provides several functions for manipulating the case of a
string.
For many types of codes, whether within the computer world or
not, text strings are expected to appear only in uppercase letters.
For example, U.S. state and Canadian province postal abbreviations
should always be uppercase. The strtoupper() function converts
all of the letters in a string to uppercase. Similarly, the strtolower()
function converts all of the letters in a string to lowercase. For

CHAPTER
3
The
ucfirst()
and
lcfirst()
functions
only change the first char-
acter of a string. The
ucwords() function
only changes the first
character of each word.
These functions do not
change the case of any
other character in a
string. To ensure that the
remaining characters in a
string are lowercase
when using the
ucfirst() and
ucwords()functions,
you need to use the
strtolower() function
on the string first. To
ensure that the remaining
characters are uppercase
when using the
lcfirst() function,
you need to use the
strtoupper() function
on the string first.
Manipulating Strings
example, this function is useful when converting a document from
HTML to XHTML, because the XHTML standard specifies that all
element and attribute tags must be lowercase.
When working with natural languages, more complex conversions
are needed. Sentences in English start with an uppercase letter.
The ucfirst() function ensures that the first character of a string
Is uppercase. If you need the reverse of ucfirst(), the lcfirst()
function converts the first character of a string to lowercase. Titles of
books, songs, poems, and articles usually have the first letter of each
word capitalized. The ucwords() function converts the first character
of each word in a string to uppercase.
Consider the following example and the output shown in Figure 3-8:
$ConfusingText = "tHIs seNTEnCE iS HArD to rEAD.";
echo "<h1>Confusing Text</h1>\n";
echo "ucfirst: " . ucfirst($ConfusingText) . "<br />\n";
echo "lcfirst: " . lcfirst($ConfusingText) . "<br />\n";
echo "ucwords: " . ucwords($ConfusingText) . "<br />\n";
$LowercaseText = strtolower($ConfusingText);
echo "<h1>Lowercase Text</h1>\n";
echo "ucfirst: " . ucfirst($LowercaseText) . "<br />\n";
echo "lcfirst: " . lcfirst($LowercaseText) . "<br />\n";
echo "ucwords: " . ucwords($LowercaseText) . "<br />\n";
136
Figure 3-8 Using the ucfirst(), lcfirst(),
and ucwords() functions
Working
with a Single String
In
the three lines under the “Confusing Text” title, it is still
very dif-
ficult
to read the text, because the strings are a mix of uppercase and
lowercase
letters. In addition, it is hard to see what changed in the
three
lines. The three lines under the “Lowercase Text” title are much
clearer
to read and it is easier to see what changed, because all of the
text
started in lowercase.
To
manipulate the case of a string:
1.
2.
Create
a new document in your text editor.
Type
the <!DOCTYPE>
declaration,
<html>
element,
document
head,
and <body>
element.
Use the strict DTD and “Word
Play”
as the content of the <title>
element.
Add
the following script section to the document body:
<?php
?>
3.
4.
Declare
and initialize a string called $StartingText,
as
follows:
$StartingText
= "mAdAm, i'M aDaM.";
5.
Add
the following four lines of code to convert and display
the
string in uppercase and lowercase:
$UppercaseText
= strtoupper($StartingText);
$LowercaseText
= strtolower($StartingText);
echo
"<p>$UppercaseText</p>\n";
echo
"<p>$LowercaseText</p>\n";
The
ucfirst()
and
ucwords()
functions do
not always capitalize a
proper name correctly,
such as in strings that
require more than one
capital letter. Consider
trying to convert the
strings “des moines”,
“mary-elizabeth”, and
“dimaggio” to the proper
names “Des Moines”,
“Mary-Elizabeth”, and
“DiMaggio”. The
ucfirst() function will
convert the strings to
“Des moines”, “Mary-
elizabeth”, and
“Dimaggio”, respectively.
The ucwords() function
will properly convert “des
moines” to “Des Moines”,
but its conversions of the
other two strings will
match those of the
ucfirst() function.
137
6.
Add the following four lines to display the text with different
mixes of uppercase and lowercase letters:
echo "<p>" . ucfirst($LowercaseText) . "</p>\n";
echo "<p>" . lcfirst($UppercaseText) . "</p>\n";
$WorkingText = ucwords($LowercaseText);
echo "<p>$WorkingText</p>\n";
7.
Save the file as WordPlay.php, upload it to the server, and
then open the file in your Web browser by entering the
following URL:
http://<yourserver>/PHP_Projects/Chapter.03/Chapter/
WordPlay.php. Figure 3-9 shows the output in your Web
browser window.

CHAPTER
3
Manipulating
Strings
138
Figure
3-9
Output
of WordPlay.php
8.
Close
your Web browser window.
Encoding
and Decoding a String
Because
of the close relationship between XHTML, the Internet, and
PHP,
several functions are built into PHP for dealing with Web pages.
The
htmlspecialchars()
and
htmlspecialchars_decode()
func-
tions
in XHTML are only useful for processing strings. XHTML has
five
reserved characters: the ampersand (&),
double quotation mark
("),
single quotation mark ('),
left angle bracket or “less than” symbol
(<),
and right angle bracket or “greater than” symbol (>).
To display
these
characters as text on an XHTML page, they should be encoded
using
HTML character entities. The htmlspecialchars()
function
converts
any occurrence of these five characters to their equivalent
HTML
character entity. Specifically, ‘&’
becomes ‘&’,
‘"’
becomes
‘"’,
‘'’
becomes ‘'’,
‘<’
becomes ‘<’,
and ‘>’
becomes
‘>’.
The htmlspecialchars_decode()
function
performs the
reverse
operation, converting the HTML character entities into their
equivalent
characters.
Passwords
are required for secure access to a Web site. Storing pass-
words
as plain text strings creates security and privacy issues. The
md5()
function
is a way to avoid storing passwords as plain text.
The
md5()
function
uses a strong encryption algorithm (called the
Message-Digest
Algorithm) to create a one-way hash of the entered
string.
A one-way
hash is
a fixed-length string based on the entered
text,
from which it is nearly impossible to determine the original text.
Because
it is a one-way hash, there is no equivalent decode function
for
the md5()
function.
In theory, a one-way hash makes it impossible
Turning
on
the PHP
configuration
setting
“ENT_
NOQUOTES” disables the
conversion of the double
quotation mark. Turning
on the PHP configuration
setting “ENT_QUOTES”
enables the conversion of
the single quotation
mark.

Working
with a Single String
to
convert the stored hash value back to the original password to
compare
against an entered password. Instead, the entered pass-
word
is passed to the md5()
function,
and the resulting hash value is
compared
against the stored hash value. If the two are the same, the
entered
password is considered to be valid.
PHP
provides a
number of
functions for
encrypting
strings using different
algorithms.
Although
converting a
one-way hash
value back to
the original
value is supposedly
impossible, hackers have
managed to “crack” many
one-way hash algorithms,
including the md5() algo-
rithm. Encryption algo-
rithms, like physical
locks, will not stop some-
one who is determined to
defeat them.
139
Other Ways to Manipulate a String
If a string has leading or trailing spaces, the trim() function will
remove them. To remove only the leading spaces, use the ltrim()
(left trim) function. To remove only the trailing spaces, use the
rtrim() (right trim) function.
To return only a portion of a string, use the substr() function.
This function takes the input string as the first parameter, the start-
ing position as the second parameter, and the length of the string to
return as an optional third parameter. For numbers that are zero or
positive, the starting position is calculated from the start of the string,
with zero being the first character. For negative numbers, the starting
position is calculated from the end of the string, with –1 being the
last character. If the length is omitted or is greater than the remain-
ing length of the string, the entire remainder of the string is returned.
Figure 3-10 shows the output of the following example:
$ExampleString = "woodworking project";
echo substr($ExampleString,4) . "<br />\n";
echo substr($ExampleString,4,7) . "<br />\n";
echo substr($ExampleString,0,8) . "<br />\n";
echo substr($ExampleString,-7) . "<br />\n";
echo substr($ExampleString,-12,4) . "<br />\n";
Figure 3-10
Some examples using the substr() function

CHAPTER
3
Manipulating
Strings
Many
more functions are available in PHP to manipulate the charac-
ters
in a string. Although they will not all be discussed in this
section,
two
deserve special mention. The strrev()
function
reverses the
order
of the characters in a string, and the str_shuffle()
function
randomly
scrambles the order.
140
To
add the md5(),
substr(),
strrev(),
and str_shuffle()
functions
to
the Word Play example:
1.
2.
Return
to the WordPlay.php script in your text editor.
Add
the following five lines before the end of the PHP block:
echo
echo
echo
echo
echo
"<p>"
"<p>"
"<p>"
"<p>"
"<p>"
.
.
.
.
.
md5($WorkingText)
. "</p>\n";
substr($WorkingText,0,6)
. "</p>\n";
substr($WorkingText,7)
. "</p>\n";
strrev($WorkingText)
. "</p>\n";
str_shuffle($WorkingText)
. "</p>\n";
3.
Save
the WordPlay.php file, upload it to the server, and then
open
the file in your Web browser by entering the following
URL:
http://<yourserver>/PHP_Projects/Chapter.03/Chapter/
WordPlay.php.
Figure 3-11 shows the new Web page.
Figure
3-11
Output
of the Word Play script
4.
Close
your Web browser window.