Chapter 23

23.1

State whether each of the following is true or false. If false, explain why.

a) PHP script is never in the same file as XHTML script.

False. PHP is directly embedded directly into XHTML.

b) PHP variable names are case sensitive.

True.

c) The settype function only temporarily changes the type of a variable.

False. Function settype permanently changes the type of a variable.

d) Conversion between data types happens automatically when a variable is used in a context that requires a different data type.

True.

e) The foreach statement is designed specifically for iterating over arrays.

True.

f) Relational operators can only be used for numeric comparison.

False. Relational operators can also be used for alphabetic comparison.

g) The quantifier +, when used in a regular expression, matches any number of the preceding pattern.

False. The quantifier + matches one or more of the preceding pattern.

h) Function die never takes arguments.

False. Function die has an optional argument -- a string to be printed as the script exits.

i) Cookies are stored on the server computer.

False. Cookies are stored on the client's computer.

j) The * arithmetic operator has higher precedence than the + operator.

True.

23.2

Fill in the blanks in each of the following statements:

a) PHP scripts typically have the file extension php.

b) The two numeric types that PHP variables can store are int or integer, float or double.

c) In PHP, uninitialized variables have the value undef.

d) Arrays are divided into elements, each of which acts like an individual variable.

e) Function count return the total number of elements in an array.

f) To use POSIX regular expressions, use the ereg functions.

g) A(n) character class in a regular expression matches a predefined set of characters.

h) Data submitted through the HTTP post method is stored in array $_POST.

i) Function die terminates script execution.

j) Cookies can be used to maintain state information on a client's computer.

23.3

Identify and correct the error in each of the following PHP code examples:

a) <?php print( "Hello world" ); >

This problem is missing "?>" to end the PHP script. ">" is not enough.

b) <?php

$name = "Paul";

print( "$Name" );

?><!-- end PHP script -->

The variable "name" can not be recalled by and uppercase "Name" for the print statement.

23.4

Write a PHP regular expression pattern that matches a string that satisfies the following description: The string must begin with the (uppercase) letter A. Any three alphanumeric characters must follow. After these, the letter B (uppercase of lowercase) must be repeated one or more times, and the string must end with two digits.

"[[:< : ]](A[[:alpha:]]{3}+[bB]+[a-zA-Z0-9]+[[:digit:]]{2})[[:>:]]"

23.5

Describe how input from an XHTML form is retrieved in a PHP program.

A user submits information to the server via a $_GET or $_POST command. The field key and values are carried into the superglobal arrays for GET or POST. To use the data a function "expand" in the php script is called to create the variable/value pairs to use within the script.

23.6

Describe how cookies can be used to store information on a computer and how the information can be retrieved by a PHP script. Assume that cookies are not disabled on the client.

Cookies are a text document with values that can be given an expiration date for when they are no longer needed. PHP can write them with the setcookie function and read them with the $_COOKIE superglobal variable.

News:

Working on Chapter 23.