HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [200]
♦ The XHTML syntax is buried inside PHP syntax. The example shows PHP code creating HTML code. Each line contains code from two languages interspersed. This can be disconcerting to a beginning programmer.
Generating output with heredocs
PHP uses a clever solution called heredocs to resolve all these issues. A heredoc is simply a type of multiline quote, usually beginning and ending with the word HERE.
The best way to understand heredocs is to see one in action, so here’s the same example written as a heredoc:
$name = “John”;
$address = “123 Main St.”;
print << HERE; ?> Figure 1-5 illustrates this code in action. Figure 1-5: This page was created with the heredoc mechanism. Heredocs have some great advantages: ♦ All carriage returns are preserved. There’s no need to put in any newline characters. Whatever carriage returns are in the original text will stay in the output. ♦ Heredocs preserve quote symbols. There’s also no need to escape your quotes because the double quote is not the end-of-string character for a heredoc. ♦ Variable interpolation is supported. You can use variable names in a heredoc, just like you do for an ordinary quoted string. ♦ The contents of a heredoc feel like ordinary XHTML. When you’re working inside a heredoc, you can temporarily put your mind in XHTML mode, but with the ability to interpolate variables. The following are some things to keep in mind about heredocs: ♦ A heredoc is opened with three less-than symbols (<<<) followed by a heredoc symbol that will act as a “superquote” (instead of single or double quotation marks, you make your own custom quotation mark from any value that you want). ♦ A heredoc symbol can be denoted by almost any text, but HERE is the most common delimiter (thus, heredoc). You can make absolutely anything you want serve as a heredoc symbol. You probably should just stick to HERE because that’s what other programmers are expecting. ♦ You need only one semicolon for the whole heredoc. Technically, the entire heredoc counts as one line. That means that the only semicolon you need is after the closing symbol. ♦ A heredoc must be closed with the same word it was opened with. ♦ The closing word for the heredoc must be on its own line. ♦ You can’t indent the closing word for the heredoc; there can’t be any spaces or tabs preceding the closing word. By far, the most common problem with heredocs is indenting the closing token. The HERE (or whatever other symbol you’re using) must be flush with the left margin of your editor, or PHP won’t recognize it. This usually means PHP interprets the rest of your program as part of a big string and never finishes executing it. Heredocs have one disadvantage: They tend to mess up your formatting because you have to indent heredocs differently than the rest of the code. When writing a heredoc, don’t put a semicolon after the first << There’s one more way to combine PHP and XHTML code. The server treats a PHP document mainly as an XHTML document. Any code not inside the symbols is treated as XHTML, and anything inside the PHP symbols is interpreted as PHP. This means you can switch in and out of PHP, like the following example: $name = “John”; $address = “123 Main St.”; // switch ‘out’ of PHP temporarily ?>name $name address $address
Switching from PHP to XHTMLname address
®Online Book Reader