Section 5.2. Embedding into HTML


5.2. Embedding into HTML

PHP doesn't have to be embedded in an HTML file, of course; you can create a PHP file that includes no HTML. However, when building a web application, you often use PHP and HTML together in a file. PHP was developed primarily for web use, to be embedded in HTML files as a templating language. When PHP code is included in a file, the file is given the PHP extension (the extension that signals your web server to expect PHP code in the file); usually .php, but a different extension(s), such as .phtml or .php5, can be specified when you configure your web server.

The following code shows PHP embedded in HTML:

 <html> <head><title>Example 1</title></head> <body> <?php     /* If it is April 1st, we show a quote */     if (date('md' == '0401')) {         echo 'A bookstore is one of the only pieces of evidence we have '.             'that people are still thinking. <i>Jerry Seinfeld</i>';     } else {         echo 'Good morning!';     } ?> </body> </html> 

The line <?php begins the PHP section embedded into the HTML code; the line ?> ends the PHP section. Notice that the code uses echo to send the output. When the text is so simple, the echo statements are acceptable. However, when you need to echo text strings that contain single or double quotes, the code becomes more complicated. If the text to be echoed in the example was a link statement (such as <a href='http...'>), the example would not have worked correctly because the single quotes in the text would conflict with the single quotes enclosing the text string. For such a case, the PHP section can be ended before the text needs to be output and begin again before the PHP code that ends the if block and starts the else bock is needed, as in the following example:

 <html> <head><title>Example 2</title></head> <body> <?php     /* If it is April 1st, we show a quote */     if (date('md' == '0401')) {         echo 'A bookstore is one of the only pieces of evidence we '.             'have that people are still thinking. <i>Jerry Seinfeld<i>';     } else {         echo 'Good morning!'; } ?>   </body>   </html> 

This coding behavior is messy. You are violating one of the principles of programming: "Separate logic from content." The following version of embedding stores the text in a variable and then echoes the variable:

 <?php     /* If it is April 1st, we show a quote */     if (date('md' == '0401')) {         $greeting = 'A bookstore is one of the only pieces of '.             'evidence we have that people are still thinking. '.             '<i>Jerry Seinfeld</i>';     } else {         $greeting = 'Good morning!';     } ?> <html> <head><title>Example 3</title></head> <body> <?php echo $greeting; ?> </body> </html> 

A shorter form of the PHP tag, <?, can usually be used instead of <?php. The php.ini configuration setting "short_tags" must be set to "on," but this is the default. However, you need to be careful using the short tags because not every server might always have short_tags turned on. Also, short_tags can conflict with XML usage because <? is the start of a processing instruction. An additional tag <?= is available, which is the equivalent of <?php echo, as the following snippet demonstrates:

 ... ... <html> <head><title>Example 4</title></head> <body> <?= $greeting; ?> </body> </html> 

If you want to be sure your application can run on as many systems as possible, you should not rely on short tags because they might be turned off. The rest of the examples in this chapter use the non-short tags everywhere. We also cover some additional techniques for separating code and layout.



    PHP 5 Power Programming
    PHP 5 Power Programming
    ISBN: 013147149X
    EAN: 2147483647
    Year: 2003
    Pages: 240

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net