Exercises from the Book 'PHP and MySQL for Dynamic Web Sites' by Larry Ullman
Submitted by: Mike Murphy, Aug 2013

Chapter 2 Completed Exercises

Chapter 2 Review

Question Answer
1. What is the significance of a form's METHOD attribute? Of its ACTION attribute? The method dictates how the data is sent to the handling page.
The GET HTTP method is customarily used for requesting information, e.g. reading a record from a database.
The POST HTTP method is used when an action is expected, e.g. writing to a database record.
GET sends submitted data to the receiving page as a series of name/value pairs appended to the URL, visible to everyone, and it has limits on the amount of information to send.
Information sent with the POST method is invisible to others and has no limits on the amount of information to send. However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

The action attribute specifies the name of the script to which the form data will be sent.
2. Why must an HTML form that gets submitted to a PHP script be loaded through a URL? What would happen upon submitting the form if it were not loaded through a URL? PHP scripts require a Web server application (e.g. Apache) to interpret them, so all PHP scripts must be accessed through a URL.

PHP is a server-side technology. All of the action takes place on the server and its scripts run only after an event triggers them. With PHP, the server reads the PHP code then decides what to send back to your Web browser. This is in contrast to HTML pages which can be displayed solely by your browser without server-side input.

3. What are the differences between using single and double quotation marks to delineate strings? Single quotes always display exactly what you type (other than escaped quotes and backslashes); double quotes, not necessarily.

Values enclosed in single quotation marks will be treated literally, whereas those within double quotation marks will be interpreted. Placing variables and special characters within double quotes will result in their represented values, not their literal values.
$foo = "the answer is $var"; will see $var replaced by its value in the Web browser.
4. What control structures were introduced in this chapter? Structures included: IF (elseif, else), SWITCH (in a sidebar), FOREACH, FOR, WHILE.
5. What new variable type was introduced in this chapter? The array: An array in PHP is an ordered map. A map is a type that associates VALUES to KEYS. One key/value pair is an item or element of the array. For each item in the list, there is a key or index associated with it.

Array values can be other arrays, trees and multidimensional arrays are also possible. Reference a value in an array by writing the array variable name, followed by the key within square brackets.
6. What operator tests for equality? What is the assignment operator? The comparison operator for equality is "==", as in $a == $b;.

The basic assignment operator is "=". This does not mean "equal to", rather it means that the left operand gets set to the value of the expression on the right.
7. Why are textual form elements validated using EMPTY() but other form elements are validated using ISSET()? Only EMPTY() properly tests for an empty string as we usually want to define it.

ISSET() tests if a variable has a value, including zero, FALSE, or an empty string (but not NULL). An empty string will return a value of true. To detect that a text variable has actually been entered, it is necessary to use EMPTY() which tests for zero, NULL, FALSE, or an empty string.
8. What is the difference between an indexed array and an associative array? Indexed arrays use integers as the keys.
Associative arrays use strings as keys.
PHP arrays can contain integer and string keys at the same time as PHP does not distinguish between indexed and associative arrays.
9. With what value do indexed arrays begin by default? If an indexed array has ten elements in it, what would the expected index be of the last element in the array? Indexed arrays begin with the first index at zero, unless the keys are explicitly specified, so an array with ten elements would be indexed at zero through nine.
10. What are the superglobal arrays? From where do the following superglobals get their values? Superglobals are predefined, built-in variables that are always available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.

$_GET - passed to the current script via the HTTP GET method, usually from a form
$_POST - passed to the current script via the HTTP POST method, usually from a form
$_COOKIE - variables passed to the current script via HTTP Cookies, used with the SETCOOKIE() function to send a cookie
$_REQUEST - an array that by default contains the contents of $_GET, $_POST and $_COOKIE.
$_SESSION - array containing session variables available to the current script. Beginning point for a range of functions such as SESSION_START().
$_SERVER - information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these
$_ENV - variables passed to the current script via the environment method. Many are provided by the shell under which PHP is running, so their contents vary.
11. How can you print an individual indexed array item? How can you print an individual associative array item? Indexed arrays can be printed by referring to the index number;
Example 1:
echo "The first state is $indxd[0].";
Example 2:
foreach ($indxd as $key => $value) {
echo "State number $key is $value\n";

To print an associative array item it is necessary to surround the array name and key with curly braces.
Example 1:
echo "The first state is {$assoc['AL']}."
Example 2:
foreach($assoc as $x=>$x_value) {
echo "Abbr=" . $x . ", Name=" . $x_value;
12. What does the COUNT() function do? It returns the number of elements in an array, or something in an object. You may count all the elements of a multidimensional array by setting the optional mode parameter to COUNT_RECURSIVE (or 1).
13. What impact does printing \n have on the Web browser? The newline escape character adds a line break in the generated HTML source. It is a good habit to ensure a clean break at the end of a text field.
14. Generally speaking, when would you use a WHILE loop? When would you use a FOR loop? When would you use a FOREACH loop? What is the syntax of each loop type? WHILE - executes a block of code while a condition is true. Use this simpler (than FOR) statement when the number of iterations of the script is unknown or less critical.

Syntax: while (condition)
{ statements }

FOR - use this in place of WHILE when you know in advance how many times the script should run. You can set the counter and increment it through parameters 1 and 3, evaluating the condition in parameter 2.

Syntax: for (initial expression 1; beginning of each loop condition 2; end of each loop action 3)
{ statements }

FOREACH - this loop will work only on arrays and objects, so it is an obvious choice to walk through a single or multi-dimensional array.

Syntax: foreach (array_expression as $value)
{ statements }
15. What is the ++ operator? What does it do? It is an incrementing operator
Pre-increment (++$a) Increments $a by one, then returns $a.
Post-increment ($a++) Returns $a, then increments $a by one.