2.7. Embedding PHP in Web PagesAlthough it is possible to write and run standalone PHP programs, most PHP code is embedded in HTML or XML files. This is, after all, why it was created in the first place. Processing such documents involves replacing each chunk of PHP source code with the output it produces when executed. Because a single file contains PHP and non-PHP source code, we need a way to identify the regions of PHP code to be executed. PHP provides four different ways to do this. As you'll see, the first, and preferred, method looks like XML. The second method looks like SGML. The third method is based on ASP tags. The fourth method uses the standard HTML <script> tag; this makes it easy to edit pages with enabled PHP using a regular HTML editor. 2.7.1. XML StyleBecause of the advent of the eXtensible Markup Language (XML) and the migration of HTML to an XML language (XHTML), the currently preferred technique for embedding PHP uses XML-compliant tags to denote PHP instructions. Coming up with tags to demark PHP commands in XML was easy, because XML allows the definition of new tags. To use this style, surround your PHP code with <?php and ?>. Everything between these markers is interpreted as PHP, and everything outside the markers is not. Although it is not necessary to include spaces between the markers and the enclosed text, doing so improves readability. For example, to get PHP to print "Hello, world", you can insert the following line in a web page: <?php echo "Hello, world"; ?> The trailing semicolon on the statement is optional, because the end of the block also forces the end of the expression. Embedded in a complete HTML file, this looks like: <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>This is my first PHP program!</title> </head> <body> <p> Look, ma! It's my first PHP program:<br /> <?php echo "Hello, world"; ?><br /> How cool is that? </p> </body> </html> Of course, this isn't very exciting—we could have done it without PHP. The real value of PHP comes when we put dynamic information from sources such as databases and form values into the web page. That's for a later chapter, though. Let's get back to our "Hello, world" example. When a user visits this page and views its source, it looks like this: <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>This is my first PHP program!</title> </head> <body> <p> Look, ma! It's my first PHP program:<br /> Hello, world!<br /> How cool is that? </p> </body> </html> Notice that there's no trace of the PHP source code from the original file. The user sees only its output. Also notice that we switched between PHP and non-PHP, all in the space of a single line. PHP instructions can be put anywhere in a file, even within valid HTML tags. For example: <input type="text" name="first_name" value="<?php echo "Rasmus"; ?>" /> When PHP is done with this text, it will read: <input type="text" name="first_name" value="Rasmus" /> The PHP code within the opening and closing markers does not have to be on the same line. If the closing marker of a PHP instruction is the last thing on a line, the line break following the closing tag is removed as well. Thus, we can replace the PHP instructions in the "Hello, world" example with: <?php echo "Hello, world"; ?> <br /> 2.7.2. SGML StyleThe "classic" style of embedding PHP comes from SGML instruction processing tags. To use this method, simply enclose the PHP in <? and ?>. Here's the "Hello world" example again: <? echo "Hello, world"; ?> This style, known as short tags, is the shortest and least intrusive, and it can be turned off so as to not clash with the XML PI (Process Instruction) tag in the php.ini initialization file. Consequently, if you want to write fully portable PHP code that you are going to distribute to other people (who might have short tags turned off ), you should use the longer <?php ... ?> style, which cannot be turned off. If you have no intention of distributing your code, you don't have an issue with telling people who want to use your code to turn on short tags, and you are not planning on mixing XML in with your PHP code, then using this tag style is okay. 2.7.3. ASP StyleBecause neither the SGML nor XML tag style is strictly legal HTML,[3] some HTML editors do not parse it correctly for color syntax highlighting, context-sensitive help, and other such niceties. Some will even go so far as to helpfully remove the "offending" code for you.
However, many of these same HTML editors recognize another mechanism (no more legal than PHP's) for embedding code—that of Microsoft's Active Server Pages (ASP). Like PHP, ASP is a method for embedding server-side scripts within documents. If you want to use ASP-aware tools to edit files that contain embedded PHP, you can use ASP-style tags to identify PHP regions. The ASP-style tag is the same as the SGML-style tag, but with % instead of ?: <% echo "Hello, world"; %> In all other ways, the ASP-style tag works the same as the SGML-style tag. ASP-style tags are not enabled by default. To use these tags, either build PHP with the --enable-asp-tags option or enable asp_tags in the PHP configuration file. 2.7.4. Script StyleThe final method of distinguishing PHP from HTML involves a tag invented to allow client-side scripting within HTML pages, the <script> tag. You might recognize it as the tag in which JavaScript is embedded. Since PHP is processed and removed from the file before it reaches the browser, you can use the <script> tag to surround PHP code. To use this method, simply specify "php" as the value of the language attribute of the tag: <script language="php"> echo "Hello, world"; </script> This method is most useful with HTML editors that work only on strictly legal HTML files and don't yet support XML processing commands. 2.7.5. Echoing Content DirectlyPerhaps the single most common operation within a PHP application is displaying data to the user. In the context of a web application, this means inserting into the HTML document information that will become HTML when viewed by the user. To simplify this operation, PHP provides special versions of the SGML and ASP tags that automatically take the value inside the tag and insert it into the HTML page. To use this feature, add an equals sign (=) to the opening tag. With this technique, we can rewrite our form example as: <input type="text" name="first_name" value="<?="Rasmus"; ?>"> If you have ASP-style tags enabled, you can do the same with your ASP tags: <p>This number (<%= 2 + 2 %>)<br /> and this number (<% echo (2 + 2); %>) <br /> Are the same.</p> After processing, the resulting HTML is: <p>This number (4) <br /> and this number (4) <br /> are the same.</p> Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|