The Building Blocks / Page 3 | WebReference

The Building Blocks / Page 3

[previous] [next]

The Building Blocks:
Data Types, Literals, Variables, and Constants

The Here Document—A Special Kind of Quoting. Here documents are a kind of quoting popular in a number of languages, such as JavaScript, Perl, Shell scripts, and so on. Here documents, also called here-docs, allow you to quote a large block of text within your script without using multiple print statements and quotes. The entire block of text is treated as though it is surrounded by double quotes. This can be useful if you have a large block of HTML within your PHP script interspersed with variables, quotes, and escape sequences.

Rules for a Here Document:
  1. The user-defined delimiter word starts and terminates the here document. Text is inserted between the delimiters. The delimiter can contain letters, numbers, and the underscore character. The first letter must be a letter or an underscore. By convention, the delimiter should be in all uppercase letters to make it stand out from other words in your script. The delimiter is preceded by three

  2. The delimiter cannot be surrounded by any spaces, comments, or other text. The final delimiter can optionally be terminated with a semicolon and must be on a line by itself.
  3. All variable and escape sequences will be interpreted within the here document.

Example 4.4


  1. PHP starts here.
  2. Two scalar variables are defined.
  3. This is the here-doc. The user-defined terminator, MY_BOUNDARY, is prepended with . There can be no space after the terminator; otherwise an error like this will be displayed: Parse error: syntax error, unexpected T_SL in c:\wamp\www\exemples\ch4variables\heredoc.php on line 4
  4. All of the HTML document is embedded in the here document. The HTML will be sent to the browser as is. Any PHP code embedded withing the HTML tags will be handled by the PHP interpreter.
  5. The value of the variable, $bgcolor, will be assigned as the background color of the page.
  6. An HTML table is started here. The value of the variable, $tablecolor, will be assigned as the background color of the table cells.
  7. The HTML document ends here, inside the here-doc.
  8. The user-defined terminator, MY_BOUNDARY, marks the end of the here document. There can be no spaces surrounding the terminator. The semicolon is optional.

Figure 4.5 Here document output.

Escape Sequences. Escape sequences consist of a backslash followed by a single character. When enclosed in double quotes, the backslash causes the interpretation of the next character to "escape" from its normal ASCII code and to represent something else (see Table 4.1). To display the escape sequences in your browser, the HTML <pre> tag can be used (see Example 4.5); otherwise, the escape sequences placed within your PHP script will not be interpreted.

Table 4.1 Escape Sequences

Escape Sequence What It Represents
\'Single quotation mark
\"Double quotation
\rReturn/line feed
\$A literal dollar sign
\70Represents the octal value
\x05Represents the hexadecimal character

Example 4.5


  1. Because this file will be displayed in a browser window, the HTML <pre> tags are used to retain spaces and tabs. If you run PHP at the command line, the escape sequences will be interpreted.
  2. The PHP program starts here with its opening tag.
  3. The escape sequences must be enclosed in double quotes. The sequences for tab (\t) and newline (\n) characters produce tabs and new lines. If a backslash is prepended with another backslash, then the backslash is treated as a literal.
  4. In this example, by preceding an octal or hexadecimal number with a backslash, its ASCII equivalent is displayed.
  5. If a string is enclosed in single quotes, escape sequences are ignored. See the output in Figure 4.6.

Figure 4.6 Escape sequences and the pre tag.
Figure 4.7 Escape sequences at the command line.

[previous] [next]