OBJECTIVES
In this chapter you will learn:
-
To manipulate data of various types.
-
To use operators, arrays and control statements.
-
To use regular expressions to search for patterns.
-
To construct programs that process form data.
-
To store data on the client using cookies.
-
To create programs that interact with MySQL databases.
Introduction
PHP, or PHP: Hypertext Preprocessor, has become one of the most popular server-side scripting languages for creating dynamic web pages. PHP was created by Rasmus Lerdorf to track users at his website. In 1995, Lerdorf released it as a package called the “Personal Home Page Tools.” Two years later, PHP 2 featured built-in database support and form handling. In 1997, PHP 3 was released with a rewritten parser, which substantially in- creased performance and led to an explosion of PHP use. The release of PHP 4 featured the new Zend Engine from Zend, a PHP software company. This version was considerably faster and more powerful than its predecessor, further increasing PHP’s popularity. It is estimated that over 15 million domains now use PHP, accounting for more than 20 per- cent of web pages.1 Currently, PHP 5 features the Zend Engine 2, which provides further speed increases, exception handling and a new object-oriented programming model^2^. More information about the Zend Engine can be found at www.zend.com.
PHP is an open-source technology that is supported by a large community of users and developers. PHP is platform independent—implementations exist for all major UNIX, Linux, Mac and Windows operating systems. PHP also supports many databases, including MySQL.
After introducing the basics of the PHP scripting language, we discuss form pro- cessing and business logic, which are vital to e-commerce applications. Next, we build a three-tier web application that queries a MySQL database. We also show how PHP can use cookies to store information on the client that can be retrieved during future visits to the website. Finally, we revisit the form-processing example to demonstrate some of PHP’s more dynamic capabilities.
PHP Basics
The power of the web resides not only in serving content to users, but also in responding to requests from users and generating web pages with dynamic content. Interactivity be- tween the user and the server has become a crucial part of web functionality, making PHP—a language written specifically for interacting with the web—a valuable tool.
Installing PHP
PHP code is embedded directly into XHTML documents, though these script segments are interpreted by the server before being delivered to the client. This allows the document author to write XHTML in a clear, concise manner. PHP script file names end with .php.
To run a PHP script, PHP must first be installed on your system. All examples and exercises in this chapter have been verified using PHP 5.2.3, the most current release at the time of publication. The most recent version of PHP can be downloaded from www.php.net/downloads.php, and installation instructions are available at www.php.net/ manual/en/installation.php. Be sure to check for specific instructions that pertain to the server you want to use. During setup, when the Choose Items to Install window is dis- played, expand the Extensions menu by clicking the small plus sign to its left. Then click the down arrow to the left of the MySQL option, and select the Will be installed on local hard drive option. This will ensure that your PHP script will be able to access your MySQL database server for examples later in this chapter.
Although PHP can be used from the command line, a web server is necessary to take full advantage of the scripting language. Before continuing, files from the Chapter 23 examples directory to the web server’s root directory (e.g., C:\Inetpub\wwwroot for IIS or C:\Program Files\Apache Software Foundation\Apache2\htdocs for Apache on Win- dows or /var/www/html or similar on Linux).
Simple PHP Program
Figure 23.1 presents a simple PHP program that displays a welcome message. In PHP, code is inserted between the scripting delimiters . PHP code can be placed anywhere in XHTML markup, as long as the code is enclosed in these delimiters. Line 1 uses function print to output the XML declaration. This avoids the <? in the XML dec-
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 23.1: first.php -->
<!-- Simple PHP program. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<?php
$name = "Harvey"; // declaration and initialization
?>
<!--end PHP script-->
<head>
<title>Using PHP document</title>
</head>
<body style = "font-size: 2em">
<p>
<strong>
<!-- print variable name’s value -->
Welcome to PHP, <?php print( "$name" ); ?>!
</strong>
</p>
</body>
</html>
Fig. 1
laration getting interpreted as an incorrect PHP scripting delimiter. Line 9 declares vari- able $name and assigns it the string “Harvey”. All variables are preceded by a $ and are created the first time they are encountered by the PHP interpreter. PHP statements ter- minate with a **semicolon (;).
Common Programming Error 1
Failing to precede a variable name with a $ is a syntax error.
Common Programming Error 2
Variable names in PHP are case sensitive. Failure to use the proper mixture of cases to refer to a variable will result in a logic error, since the script will create a new variable for any name it doesn’t recognize as a previously used variable. 23.2
Common Programming Error 3
Forgetting to terminate a statement with a semicolon (;) is a syntax error. 23.3
Line 9 also contains a single-line comment, which begins with two forward slashes (//). Text to the right of the slashes is ignored by the interpreter. Single-line comments can also begin with the pound sign (#). Multiline comments begin with delimiter /* and end with delimiter */.
Line 18 outputs the value of variable $name by calling function print. The actual value of $name is printed, not the string “$name”. When a variable is encountered inside a double-quoted ("") string, PHP interpolates the variable. In other words, PHP inserts the variable’s value where the variable name appears in the string. Thus, variable $name is replaced by Harvey for printing purposes. All operations of this type execute on the server before the XHTML document is sent to the client. You can see by viewing the source of a PHP document that the code sent to the client does not contain any PHP code.
PHP variables are loosely typed—they can contain different types of data (e.g., inte- gers, doubles or strings) at different times. Figure 23.2 introduces these data types.
Type | Description |
---|---|
int, integer | Whole numbers (i.e., numbers without a decimal point). |
float, double, real | Real numbers (i.e., numbers containing a decimal point). |
string | Text enclosed in either single (’’) or double ("") quotes. [Note: Using double quotes allows PHP to recognize more escape sequences.] |
bool, boolean | True or false. |
array | Group of elements. |
object | Group of associated data and methods. |
resource | An external source—usually information from a database. |
NULL | No value. |
Figure 2 | PHP Types.
Converting Between Data Types
Converting between different data types may be necessary when performing arithmetic operations with variables. Type conversions can be performed using function settype. Figure 23.3 demonstrates type conversion of some types introduced in Fig. 23.2.
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 23.3: data.php -->
<!-- Data type conversion. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Data type conversion</title>
</head>
<body>
<?php
// declare a string, double and integer
$testString = "3.5 seconds";
$testDouble = 79.2;
$testInteger = 12;
?><!-- end PHP script -->
<!-- print each variable’s value and type -->
<?php
print( "$testString is a(n) " . gettype( $testString )
. "<br />" );
print( "$testDouble is a(n) " . gettype( $testDouble )
. "<br />" );
print( "$testInteger is a(n) " . gettype( $testInteger)
. "<br />" );
?><!-- end PHP script -->
<br />
converting to other data types:<br />
<?php
// call function settype to convert variable
// testString to different data types
print( "$testString" );
settype( $testString, "double" );
print( " as a double is $testString <br />" );
print( "$testString" );
settype( $testString, "integer" );
print( " as an integer is $testString <br />" );
settype( $testString, "string" );
print( "converting back to a string results in
$testString <br /><br />" );
// use type casting to cast variables to a different type
$data = "98.6 degrees";
print( "before casting, $data is a " .
gettype( $data ) . "<br /><br />" );
print( "using type casting instead: <br />
as a double: " . (double) $data . "<br />as an integer: " . (integer) $data );
print( "<br /><br />after casting, $data is a " .
gettype( $data ) );
?><!-- end PHP script -->
</body>
</html>
Fig. 3 | Data type conversion.
Lines 14–16 of Fig. 23.3 assign a string to variable $testString, a floating-point number to variable $testDouble and an integer to variable $testInteger. Variables are automatically converted to the type of the value they are assigned. For example, variable $testString becomes a string when assigned the value “3.5 seconds”. Lines 22–27 print the value of each variable and their types using function gettype, which returns the current type of its argument. Note that when a variable is in a print statement but not part of a string, enclosing the variable name in double quotes is unnecessary. Lines 35, 38 and 40 call settype to modify the type of each variable. Function settype takes two argu- ments—the variable whose type is to be changed and the variable’s new type.
Calling function settype can result in loss of data. For example, doubles are truncated when they are converted to integers. When converting from a string to a number, PHP uses the value of the number that appears at the beginning of the string. If no number appears at the beginning, the string evaluates to 0. In line 35, the string “3.5 seconds” is converted to a double, storing 3.5 in variable $testString. In line 38, double 3.5 is converted to integer 3. When we convert this variable to a string (line 40), the variable’s value becomes “3”—much of the original content from the variable’s declaration in line 14 is lost.
Another option for conversion between types is casting (or type casting). Unlike settype, casting does not change a variable’s content—it creates a temporary copy of a variable’s value in memory. Lines 48–49 cast variable $data’s value (declared in line 17) from a string to a double and an integer. Casting is useful when a different type is required in a specific operation but you would like to retain the variable’s original value and type. Lines 45–51 show that the type and value of $data remain unchanged even after it has been cast several times.
The concatenation operator (.) combines multiple strings in the same print state- ment, as demonstrated in lines 45–51. A print statement may be split over multiple lines—all data that is enclosed in the parentheses and terminated by a semicolon is printed to the XHTML document.
Error-Prevention Tip 1
Function print can be used to display the value of a variable at a particular point during a pro- gram’s execution. This is often helpful in debugging a script.
Arithmetic Operators
PHP provides several arithmetic operators, which we demonstrate in Fig. 23.4. Line 13 declares variable $a and assigns to it the value 5. Line 17 calls function define to create a named constant. Function define takes two arguments—the name and value of the con- stant. An optional third argument accepts a bool value that specifies whether the constant is case insensitive—constants are case sensitive by default.
Common Programming Error 4
Assigning a value to a constant after it is declared is a syntax error.
Line 20 adds constant VALUE to variable $a. Line 25 uses the *multiplication assign- ment operator = to yield an expression equivalent to $a = $a * 2 (thus assigning $a the value 20). Arithmetic assignment operators—like the ones described in Chapter 7—are syntactical shortcuts. Line 33 adds 40 to the value of variable $a.
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 23.4: operators.php -->
<!-- Using arithmetic operators. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Using arithmetic operators</title>
</head>
<body>
<?php
$a = 5;
print( "The value of variable a is $a <br />" );
// define constant VALUE
define( "VALUE", 5 );
// add constant VALUE to variable $a
$a = $a + VALUE;
print( "Variable a after adding constant VALUE
is $a <br />" );
// multiply variable $a by 2
$a *= 2;
print( "Multiplying variable a by 2 yields $a <br />" );
// test if variable $a is less than 50
if ( $a < 50 )
print( "Variable a is less than 50 <br />" );
// add 40 to variable $a
$a += 40;
print( "Variable a after adding 40 is $a <br />" );
// test if variable $a is 50 or less
if ( $a < 51 )
print( "Variable a is still 50 or less<br />" );
// test if variable $a is between 50 and 100, inclusive
elseif ( $a < 101 )
print( "Variable a is now between 50 and 100,
inclusive<br />" );
else
print( "Variable a is now greater than 100 <br />" );
// print an uninitialized variable
print( "Using a variable before initializing:
$nothing <br />" ); // nothing evaluates to ""
// add constant VALUE to an uninitialized variable
$test = $num + VALUE; // num evaluates to 0
print( "An uninitialized variable plus constant
VALUE yields $test <br />" );
// add a string to an integer
$str = "3 dollars";
$a += $str;
print( "Adding a string to variable a yields $a <br />" );
?><!-- end PHP script -->
</body>
</html>
Fig. 23.4 | Using arithmetic operators.
Uninitialized variables have the value undef, which evaluates to different values, depending on its context. For example, when undef is used in a numeric context (e.g., $num in line 52), it evaluates to 0. In contrast, when undef is interpreted in a string context (e.g., $nothing in line 49), it evaluates to an empty string ("").
Error-Prevention Tip 2
Initialize variables before they are used to avoid subtle errors. For example, multiplying a num- ber by an uninitialized variable results in 0. 23.4
Strings are automatically converted to integers or doubles when they are used in arith- metic operations. In line 58, a copy of the value of variable str, “3 dollars”, is converted to the integer 3 for use in the calculation. The type and value of variable $str are left unchanged.
Keywords (examples from Fig. 23.4 include if, elseif and else) may not be used as identifiers. Figure 23.5 lists all keywords.
Initializing and Manipulating Arrays
PHP provides the capability to store data in arrays. Arrays are divided into elements that behave as individual variables. Array names, like other variables, begin with the $ symbol. Figure 23.6 demonstrates initializing and manipulating arrays. Individual array elements are accessed by following the array’s variable name with an index enclosed in square brack- ets ([]). If a value is assigned to an array that does not exist, then the array is created (line 15). Likewise, assigning a value to an element where the index is omitted appends a new element to the end of the array (line 18). The for statement (lines 21–22) prints each element’s value. Function count returns the total number of elements in the array. In this example, the for statement terminates when the counter ($i) is equal to the number of array elements.
Line 28 demonstrates a second method of initializing arrays. Function array creates an array that contains the arguments passed to it. The first item in the argument list is stored as the first array element (recall that the first element’s index is 0), the second item is stored as the second array element and so on. Lines 30–31 display the array’s contents.
Column 1 | Column 2 | Column 3 |
---|---|---|
John | Doe | Male |
Mary | Smith | Female |
PHP keywords | ||||
---|---|---|---|---|
abstract | die | exit | interface | require |
and | do | extends | isset | require_once |
array | echo | __FILE__ | __LINE__ | return |
as | else | file | line | static |
break | elseif | final | list | switch |
case | empty | for | __METHOD__ | throw |
catch | enddeclare | foreach | method | try |
__CLASS__ | endfor | __FUNCTION__ | new | unset |
class | endforeach | function | or | use |
clone | endif | global | php_user_filter | var |
const | endswitch | if | while | |
continue | endwhile | implements | private | xor |
declare | eval | include | protected | |
default | exception | include_once | public |
Fig. 23.5 | PHP keywords.
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 23.6: arrays.php -->
<!-- Array manipulation. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Array manipulation</title>
</head>
<body>
<?php
// create array first
print( "<strong>Creating the first array</strong><br />" );
$first[ 0 ] = "zero";
$first[ 1 ] = "one";
$first[ 2 ] = "two";
$first[] = "three";
// print each element’s index and value
for ( $i = 0; $i < ; $i++ )
print( "Element $i is $first[$i] <br />" );
print( "<br /><strong>Creating the second array
</strong><br />" );
// call function array to create array second
$second =
for ( $i = 0; $i < count( $second ); $i++ )
print( "Element $i is $second[$i] <br />" );
print( "<br /><strong>Creating the third array
</strong><br />" );
// assign values to entries using nonnumeric indices
$third[ "Amy" ] = 21;
$third[ "Bob" ] = 18;
$third[ "Carol" ] = 23;
// iterate through the array elements and print each
// element’s name and value
for ( reset( $third ); $element = key( $third ); next( $third ) )
print( "$element is $third[$element] <br />" );
print( "<br /><strong>Creating the fourth array
</strong><br />" );
// call function array to create array fourth using
// string indices
$fourth = array(
"January" => "first", "February" => "second",
"March" => "third", "April" => "fourth",
"May" => "fifth", "June" => "sixth",
"July" => "seventh", "August" => "eighth",
"September" => "ninth", "October" => "tenth",
"November" => "eleventh","December" => "twelfth"
);
// print each element’s name and value
foreach ( $fourth as $element => $value )
print( "$element is the $value month <br />" );
?><!-- end PHP script -->
</body>
</html>
Fig. 23.6 | Array manipulation.
In addition to integer indices, arrays can have float or nonnumeric indices (lines 37– 39). An array with noninteger indices is called an associative array. For example, indices Amy, Bob and Carol are assigned the values 21, 18 and 23, respectively.
PHP provides functions for iterating through the elements of an array (line 43). Each array has a built-in internal pointer, which points to the array element currently being ref- erenced. Function reset sets the internal pointer to the first array element. Function key returns the index of the element currently referenced by the internal pointer, and function next moves the internal pointer to the next element and returns the element. In our script, the for statement continues to execute as long as function key returns an index. Function next returns false when there are no more elements in the array. When this occurs, func- tion key cannot return an index, $element is set to false and the for statement termi- nates. Line 44 prints the index and value of each element.
The array $fourth is also associative. To override the automatic numeric indexing performed by function array, you can use operator =>, as demonstrated in lines 51–58. The value to the left of the operator is the array index and the value to the right is the ele- ment’s value.
The foreach control statement (lines 61–62) is specifically designed for iterating through arrays, especially associative arrays, because it does not assume that the array has consecutive integer indices that start at 0. The foreach statement starts with the array to iterate through, followed by the keyword as, followed by two variables—the first is assigned the index of the element, and the second is assigned the value of that index. (If there is only one variable listed after as, it is assigned the value of the array element.) We use the foreach statement to print the index and value of each element in array $fourth.
String Processing and Regular Expressions
PHP can process text easily and efficiently, enabling straightforward searching, substitu- tion, extraction and concatenation of strings. Text manipulation is usually done with reg- ular expressions—a series of characters that serve as pattern-matching templates (or search criteria) in strings, text files and databases.
Comparing Strings
Many string-processing tasks can be accomplished by using the equality and comparison operators, demonstrated in Fig. 23.7. Line 14 declares and initializes array $fruits. Lines 17–36 iterate through each element in the $fruits array.
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 23.7: compare.php -->
<!-- Using the string-comparison operators. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>String Comparison</title>
</head>
<body>
<?php
// create array fruits
$fruits = array( "apple", "orange", "banana" );
// iterate through each array element
for ( $i = 0; $i < count( $fruits ); $i++ )
{
// call function strcmp to compare the array element
// to string "banana"
if ( strcmp( $fruits[ $i ], "banana" ) < 0 )
print( $fruits[ $i ] . " is less than banana " );
elseif ( strcmp( $fruits[ $i ], "banana" ) > 0 )
print( $fruits[ $i ] . " is greater than banana " );
else
print( $fruits[ $i ] . " is equal to banana " );
// use relational operators to compare each element
// to string "apple"
if ( $fruits[ $i ] < "apple" )
print( "and less than apple! <br />" );
elseif ( $fruits[ $i ] > "apple" )
print( "and greater than apple! <br />" );
elseif ( $fruits[ $i ] == "apple" )
print( "and equal to apple! <br />" );
} // end for
?><!-- end PHP script -->
</body>
</html>
Fig. 23.7 | Using the string-comparison operators.
Lines 21 and 23 call function strcmp to compare two strings. The function returns -1 if the first string alphabetically precedes the second string, 0 if the strings are equal, and 1 if the first string alphabetically follows the second. Lines 21–26 compare each element in the $fruits array to the string “banana”, printing whether each is greater than, less than or equal to the string.
Relational operators (==, !=, <, <=, > and >=) can also be used to compare strings. Lines 30–35 use relational operators to compare each element of the array to the string “apple”.
Regular Expressions
Functions ereg and preg_match use regular expressions to search a string for a specified pattern. Function ereg recognizes Portable Operating System Interface (POSIX) extend- ed regular expressions, while function preg_match provides Perl-compatible regular ex- pressions (PCRE). To use preg_match, you must install the PCRE library on your web server and add support for the library to PHP. More information on PCRE can be found at www.pcre.org. PHP 5 supports POSIX regular expressions, so we use function ereg in this section. Figure 23.8 demonstrates regular expressions.
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 23.8: expression.php -->
<!-- Regular expressions. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Regular expressions</title>
</head>
<body>
<?php
$search = "Now is the time";
print( "Test string is: '$search'<br /><br />" );
// call ereg to search for pattern 'Now' in variable search
if ( ereg( "Now", $search ) )
print( "String 'Now' was found.<br />" );
// search for pattern 'Now' in the beginning of the string
if ( ereg( "^Now", $search ) )
print( "String 'Now' found at beginning
of the line.<br />" );
// search for pattern 'Now' at the end of the string
if ( ereg( "Now$", $search ) )
print( "String 'Now' was found at the end
of the line.<br />" );
// search for any word ending in 'ow'
if ( ereg( "[[:<:]]([a-zA-Z]*ow)[[:>:]]", $search, $match ) )
print( "Word found ending in 'ow': " .
$match[ 1 ] . "<br />" );
// search for any words beginning with 't'
print( "Words beginning with 't' found: ");
while ( eregi( "[[:<:]](t[[:alpha:]]+)[[:>:]]",
$search, $match ) )
{
print( $match[ 1 ] . " " );
// remove the first occurrence of a word beginning
// with 't' to find other instances in the string
$search = ereg_replace( $match[ 1 ], "", $search );
} // end while
?><!-- end PHP script -->
</body>
</html>
Fig. 23.8 | Regular expressions.
Searching for Expressions
Line 13 assigns the string “Now is the time” to variable $search. The condition in line 17 calls function ereg to search for the literal characters “Now” inside variable $search. If the pattern is found, ereg returns the length of the matched string—which evaluates to true in a boolean context—and line 18 prints a message indicating that the pattern was found. We use single quotes (’’) inside the string in the print statement to emphasize the search pattern. Anything enclosed in single quotes is not interpolated (unless the single quotes are nested in a double-quoted string literal, as in line 14). For example, ‘$name’ in a print statement would output $name, not variable $name’s value.
Function ereg takes two arguments—a regular expression pattern to search for and the string to search. Although case mixture is often significant in patterns, PHP provides function eregi for specifying case-insensitive pattern matches.
Representing Patterns
In addition to literal characters, regular expressions can include metacharacters that spec- ify patterns. Examples of metacharacters include the ^, $ and . characters. The caret (^) metacharacter matches the beginning of a string (line 21), while the dollar sign ($) match- es the end of a string (line 26). The period (.) metacharacter matches any single character. Line 21 searches for the pattern “Now” at the beginning of $search. Line 26 searches for “Now” at the end of the string. Since the pattern is not found in this case, the body of the if statement (lines 27–28) does not execute. Note that Now$ is not a variable—it is a pat- tern that uses $ to search for the characters “Now” at the end of a string.
Line 31 searches (from left to right) for the first word ending with the letters ow. Bracket expressions are lists of characters enclosed in square brackets ([]) that match any single character from the list. Ranges can be specified by supplying the beginning and the end of the range separated by a dash (-). For instance, the bracket expression [a-z] matches any lowercase letter and [A-Z] matches any uppercase letter. In this example, we combine the two to create an expression that matches any letter. The special bracket expressions [[:<:]] and [[:>:]] match the beginning and end of a word, respectively.
The expression [a-zA-Z] * ow inside the parentheses represents any word ending in ow. The quantifier ** matches the preceding pattern zero or more times. Thus, [a-zA-Z] ow matches any number of letters followed by the literal characters ow. Quantifiers are used in regular expressions to denote how often a particular character or set of characters can appear in a match. Some PHP quantifiers are listed in Fig. 23.9.
Quantifier | Matches |
---|---|
{n} | Exactly n times. |
{m,n} | Between m and n times, inclusive. |
{n,} | 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. 23.9 | Some PHP quantifiers.
Finding Matches
The optional third argument to function ereg is an array that stores matches to the regular expression. When the expression is broken down into parenthetical sub-expressions, func- tion ereg stores the first encountered instance of each expression in this array, starting from the leftmost parenthesis. The first element (i.e., index 0) stores the string matched for the entire pattern. The match to the first parenthetical pattern is stored in the second array element, the second in the third array element and so on. If the parenthetical pattern is not encountered, the value of the array element remains uninitialized. Because the state- ment in line 31 is the first parenthetical pattern, Now is stored in variable $match[1] (and, because it is the only parenthetical statement in this case, it is also stored in $match[0]).
Searching for multiple instances of a single pattern in a string is slightly more compli- cated, because the ereg function returns only the first instance it encounters. To find mul- tiple instances of a given pattern, we must make multiple calls to ereg, and remove any matched instances before calling the function again. Lines 38–46 use a while statement and the ereg_replace function to find all the words in the string that begin with t. We’ll say more about this function momentarily.
Character Classes
The pattern in line 38, [[:<:]](t[[:alpha:]]+)[[:>:]], matches any word beginning with the character t followed by one or more letters. The pattern uses the character class [[:alpha:]] to recognize any letter—this is equivalent to the [a-zA-Z]. Figure 23.10 lists some character classes that can be matched with regular expressions.
Character classes are enclosed by the delimiters [: and :]. When this expression is placed in another set of brackets, such as [[:alpha:]] in line 38, it is a regular expression matching a single character that is a member of the class. A bracketed expression con- taining two or more adjacent character classes in the class delimiters represents those char- acter sets combined. For example, the expression [[:upper:][:lower:]]* represents all strings of uppercase and lowercase letters in any order, while [[:upper:]][[:lower:]]matches strings with a single uppercase letter followed by any number of lowercase char- acters. Also, note that ([[:upper:]][[:lower:]]) is an expression for all strings that alternate between uppercase and lowercase characters (starting with uppercase and ending with lowercase).
Character class | Description |
---|---|
alnum | Alphanumeric characters (i.e., letters [a-zA-Z] or digits [0-9]). |
alpha | Word characters (i.e., letters [a-zA-Z]). |
digit | Digits. |
space | White space. |
lower | Lowercase letters. |
upper | Uppercase letters. |
Fig. 23.10 | Some PHP character classes.
Finding Multiple Instances of a Pattern
The quantifier + matches one or more consecutive instances of the preceding expression. The result of the match is stored in $match[1]. Once a match is found, we print it in line 41. We then remove it from the string in line 45, using function ereg_replace. This function takes three arguments—the pattern to match, a string to replace the matched string and the string to search. The modified string is returned. Here, we search for the word that we matched with the regular expression, replace the word with an empty string, then assign the result back to $search. This allows us to match any other words beginning with the character t in the string and print them to the screen.
Form Processing and Business Logic
Superglobal Arrays
Knowledge of a client’s execution environment is useful to system administrators who want to access client-specific information such as the client’s web browser, the server name or the data sent to the server by the client. One way to obtain this data is by using a su- perglobal array. Superglobal arrays are associative arrays predefined by PHP that hold vari- ables acquired from user input, the environment or the web server, and are accessible in any variable scope. Some of PHP’s superglobal arrays are listed in Figure 23.11.
Superglobal arrays are useful for verifying user input. The arrays $_GET and $_POST retrieve information sent to the server by HTTP get and post requests, respectively, making it possible for a script to have access to this data when it loads another page. For example, if data entered by a user into a form is posted to a script, the $_POST array will contain all of this information in the new script. Thus, any information entered into the form can be accessed easily from a confirmation page, or a page that verifies whether fields have been entered correctly.
Variable name | Description |
---|---|
$_SERVER | Data about the currently running server. |
$_ENV | Data about the client’s environment. |
$_GET | Data sent to the server by a get request. |
$_POST | Data sent to the server by a post request. |
$_COOKIE | Data contained in cookies on the client’s computer. |
$GLOBALS | Array containing all global variables. |
Fig. 23.11 | Some useful superglobal arrays.
Using PHP to Process XHTML Forms
XHTML forms enable web pages to collect data from users and send it to a web server for processing. Such capabilities allow users to purchase products, request information, send and receive web-based e-mail, create profiles in online networking services and take advan- tage of various other online services. The XHTML form in Fig. 23.12 gathers information to add a user to a mailing list.
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 23.12: form.html -->
<!-- XHTML form for gathering user input. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Sample form to take user input in XHTML</title>
<style type = "text/css">
.prompt { color: blue;
font-family: sans-serif;
font-size: smaller }
</style>
</head>
<body>
<h1>Sample Registration Form</h1>
<p>Please fill in all fields and click Register.</p>
<!-- post form data to form.php -->
<form method = "post" action = "form.php">
<div>
<img src = "images/user.gif" alt = "User" /><br />
<span class = "prompt">
Please fill out the fields below.<br />
</span>
<!-- create four text boxes for user input -->
<img src = "images/fname.gif" alt = "First Name" />
<input type = "text" name = "fname" /><br />
<img src = "images/lname.gif" alt = "Last Name" />
<input type = "text" name = "lname" /><br />
<img src = "images/email.gif" alt = "Email" />
<input type = "text" name = "email" /><br />
<img src = "images/phone.gif" alt = "Phone" />
<input type = "text" name = "phone" /><br />
<span style = "font-size: 10pt">
Must be in the form (555)555-5555</span>
<br /><br />
<img src = "images/downloads.gif"
alt = "Publications" /><br />
<span class = "prompt">
Which book would you like information about?
</span><br />
<!-- create drop-down list containing book names --> <select name = "book">
<option>Internet and WWW How to Program 4e</option>
<option>C++ How to Program 6e</option>
<option>Java How to Program 7e</option>
<option>Visual Basic 2005 How to Program 3e</option>
</select>
<br /><br />
<img src = "images/os.gif" alt = "Operating System" />
<br /><span class = "prompt">
Which operating system are you currently using?
<br /></span>
<!-- create five radio buttons -->
<input type = "radio" name = "os" value = "Windows XP"
checked = "checked" /> Windows XP
<input type = "radio" name = "os" value =
"Windows Vista" /> Windows Vista<br />
<input type = "radio" name = "os" value =
"Mac OS X" /> Mac OS X
<input type = "radio" name = "os" value = "Linux" /> Linux
<input type = "radio" name = "os" value = "Other" />
Other<br />
<!-- create a submit button -->
<input type = "submit" value = "Register" />
</div>
</form>
</body>
</html>
Fig. 23.12 | XHTML form for gathering user input.
The form’s action attribute (line 21) indicates that when the user clicks the Register button, the form data will be posted to form.php (Fig. 23.13) for processing. Using method = “post” appends form data to the browser request that contains the protocol (i.e., HTTP) and the URL of the requested resource (specified by the action attribute). Scripts located on the web server’s machine can access the form data sent as part of the request.
We assign a unique name (e.g., email) to each of the form’s controls. When Register is clicked, each field’s name and value are sent to the web server. Script form.php accesses the value for each field through the superglobal array $_POST, which contains key/value pairs corresponding to name/value pairs for variables submitted through the form. [Note: The superglobal array $_GET would contain these key/value pairs if the form had been sub- mitted using the HTTP get method. In general, get is not as secure as post, because it appends the information directly to the URL, which is visible to the user.] Figure 23.13 processes the data posted by form.html and sends XHTML back to the client.
Good Programming Practice 23.1
Use meaningful XHTML object names for input fields. This makes PHP scripts that retrieve form data easier to understand. 23.1
Function extract (line 29 in Fig. 23.13) creates a variable/value pair corresponding to each key/value pair in the associative array passed as an argument (i.e., $_POST). This creates variables whose respective names and values correspond to the names and values of each posted form field. For example, line 36 in Fig. 23.12 creates an XHTML text box with the name email. In line 70 of our PHP script (Fig. 23.13), after having called func- tion extract, we access the field’s value by using variable $email. Elements in $_POST can also be accessed using standard array notation. For example, we could have accessed the form field email’s value by referring to $_POST[’email’].
1 ’ ) ?> 2 4 5 6 7 8
910 26 27
28 **36** Invalid phone number**37** A valid phone number must be in the form **38** (555)555-5555
**39** **40** Click the Back button, enter a valid phone **41** number and resubmit.
**42** Thank You." ); **43** // terminate script execution **44** } **45** ?> 46
Hi 47 48 49 . 50 Thank you for completing the survey.
51 You have been added to the 52 53 54 55 mailing list. 56
The following information has been saved 58 in our database:
59Name | 62Phone | 64OS | 65|
**71** | $phone | **72**$os | " ); **73** ?> 74
77