Mixing In Some HTML


PHP files are most often mixtures of HTML and PHP scripts. Putting HTML and PHP scripts into the same document is no problem because you put your PHP statements inside a script bounded by <?php and ?>, which means the server can pick them out easily.

You already know how the HTML in web pages work; when you insert some HTML that displays text, for example, that text is displayed when the web server reaches the line with the text in the web page as it's sending the web page back to the browser. In the same way, if a PHP script creates some text to be inserted in the web page sent back to the browser, that text is inserted into the web page at the location of that script.

For instance, take a look at Example 1-1, phphtml.php. This file contains a mix of HTML and PHP. As you can see, the HTML sets the title of the document (which will appear in the browser's title bar) and uses an <H1> HTML header to display the text Mixing HTML and PHP! in large bold font.

Example 1-1. Mixing HTML and PHP
 <HTML>     <HEAD>         <TITLE>             Mixing HTML and PHP!         </TITLE>     </HEAD>     <BODY>         <H1>             Mixing HTML and PHP!         </H1>         <?php             phpinfo();         ?>     </BODY> </HTML> 

After the <H1> header HTML element, the server will encounter our PHP script, which means that the output of the phpinfo function will be inserted into the web page we're sending back to the browser exactly at that point. As you know, the phpinfo function creates the HTML for a table holding information about the PHP installation, so the result is what you see in Figure 1-4the HTML header appears first, followed by the PHP information table.

Figure 1-4. Mixing PHP and HTML.


Want to make your PHP page look more official? You can find some "Powered by PHP" logos at http://www.php.net/download-logos.php. Just download them and add them to your web page using an <IMG> element like this:

 <HTML>     <HEAD>         <TITLE>             Mixing HTML and PHP!         </TITLE>     </HEAD>     <BODY>         <H1>             Mixing HTML and PHP!         </H1>         <?php             phpinfo();         ?>         <IMG src="/books/1/265/1/html/2/php-power-white.gif">     </BODY> </HTML> 

You can see an example in Figure 1-5, where we've downloaded php-power-white.gif and are displaying it in a PHP-enabled web page.

Figure 1-5. Adding a PHP logo.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

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