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

Chapter 1 Completed Exercises

Chapter 1 Review

Question Answer
1. What tags are used to surround PHP code? The beginning tag is: <?php
The ending tag is: ?>

A PHP script can be placed anywhere in the document. Anything written between these tags will be considered PHP by the Web server; Any text outside the tags is sent immediately to the Web browser as regular HTML.

When PHP parses a file, it looks for these opening and closing tags which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
2. What extension should a PHP file have? The default file extension for PHP files is ".php". Most Web servers use .php for PHP files and .html for standard HTML pages.
3. What does a page's encoding refer to? What impact does the encoding have on the page? Encoding dictates what characters can be represented, i.e. what language can be used. Properly encoded Web pages declare the encoding to a browser through a meta tag in the header. Without this tag, a browser may not know to switch to the proper encoding and characters may be displayed as gibberish. UTF-8 (8-bit Unicode Transformation Format) enables every symbol in every alphabet. The encoding indicated by the XHTML page must match the actual encoding set in your text editor or IDE.

Unicode example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

XHTML Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
4. What PHP functions, or language constructs, can you use to send data to the Web browser? ECHO is a language construct that outputs one or more strings. It returns no value. Since it is a construct, you are not required to use parentheses with it. Echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses. The syntax is echo expression [, expression[, expression] ... ].

PRINT is also a language construct; you are not required to use parentheses with its argument list, but it behaves more like a function and always returns 1. It outputs a string. Unlike echo, print takes only one parameter.
5. How does using single versus double quotation marks differ in creating or printing 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 printed, not their literal values.
Echo "the answer is $var"; will see $var replaced by its value in the Web browser.
6. What does it mean to escape a character in a string? It is a technique to prevent PHP from ending your strings too early, or for making sure you have the correct string information returned.

In PHP, preceding a special character with a backslash creates an escape sequence, signifying that the character after the escape character should be treated specially. Usually it causes it to be printed like any other character.
Example: $MyString = "This is an \"escaped\" string";
7. What are the three comment syntaxes in PHP? Which one can be used over multiple lines? 1. The pound or number symbol (#). Example: # This is a comment.

2. Two slashes (//) Example: // This is also a comment.
Both of these are for a single line, causing PHP to ignore everything that follows until the end of the line.

3. Comments bracketed by /* ... */ can span multiple lines.
Example: /* This is a comment
that spans two lines. */
8. What character do all variable names begin with? What characters can come next? What other characters can be used in a variable's name? Variables in PHP are represented by a dollar sign, followed by the name of the variable.
A valid variable name starts with a letter or underscore.
The remaining characters in the name may be any combination of letters, numbers, or underscores.
9. Are variable names case-sensitive or case-insensitive? In PHP, variable names are case-sensitive.
10. What is the assignment operator? The basic assignment operator is "=". It does NOT mean "equal to". It actually means that the left operand gets set to the value of the expression on the right
11. How do you create a string variable? Assign a string value to a valid variable name.

One way to specify a string value is to enclose it in single quotes. To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\).

Alternately, enclose the string in double-quotes, with the difference being that variable names will be expanded and PHP will interpret more escape sequences for special characters (e.g. linefeed, carriage return).
12. What is the concatenation operator? What is the concatenation assignment operator? There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.
13. How are constants defined and used? Create a constant with the DEFINE() function, not the assignment operator. Since they are not variables, constants do not begin with an initial dollar sign. They can only be given scalar values that cannot be changed. A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

Constants are not resolved; they are not printed within double quotation marks. Simply use the constant name to reference its value, although it may be concatenated to quoted strings or other constants.