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:- 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
- 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.
- All variable and escape sequences will be interpreted within the here document.
Example 4.4
Explanation
|
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 |
\t | Tab |
\n | Newline |
\r | Return/line feed |
\$ | A literal dollar sign |
\\ | Backslash |
\70 | Represents the octal value |
\x05 | Represents the hexadecimal character |
Example 4.5
Explanation
|
[previous] [next]
URL: