• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. Content > Adding External Content with PHP

Adding External Content with PHP

Like server-side includes, PHP allows you to tell the computer serving the Web page how to process it before it’s sent to the visitor’s browser. This allows you not only to add content, but as with JavaScript, to dynamically create the page. However, since it is server-side, your server must support PHP, which is not a given. If you aren’t sure, check with your Web site host.

One advantage of PHP is that the lion’s share of the dynamic work is performed on the server (which is most likely faster than the visitor’s computer), but it is also scriptable like JavaScript. We’ll only be dipping our toes into the sea of PHP’s possibilities here (Chapter 10 goes into a bit more detail), but it’s important to see PHP as an alternative to iframes, server-side includes, and JavaScript as a method for embedding external content files.

To add external content using PHP:


Create a new file and save it using the .php rather than .html extension (Code 4.8). The .php extension alerts the server that it should look on this page for PHP code, but you can also include any HTML you want on this page. Steps 2–4 apply to this page.

Code 4.8. Adding the .php extension for index.php alerts the Web server that this page requires special processing before it is sent out to the visitor's browser.

→ Transitional//EN" "http://www.w3.org/TR/
<html xmlns="http://www.w3.org/1999/xhtml"
→ xml:lang="en">
        <meta http-equiv="content-type"
        → content="text/html;charset=utf-8" />
        <title>DHTML &amp; CSS Advanced |
        → Using PHP to Add External Content</
        → title>


To insert PHP code on the HTML page, use the <?php open tag. Notice, though, that unlike HTML tags, there is no closing chevron (>). That comes later.


Add the require command with the path of the file you want to insert at this point in your HTML. You can include as many require statements as you desire here to mix files from multiple locations.


Close your PHP tag. You can repeat steps 2–4 anywhere on the page, as many times as you want.


Create a new file, also saving it with the .php extension (Code 4.9). This page contains all of the HTML and content you want to import—but not the <html>, <head>, and <body> tags—and can even include additional PHP code if needed (Figure 4.14).

Figure 4.14. Like the server-side include, PHP actually embeds content before the HTML document is sent to the browser so that it’s an integral part of the page. The only way to tell that this is not a standard HTML document is the .php extension on the filename.

Code 4.9. The external PHP file can contain any content, HTML, JavaScript, or PHP code you need.

<div style="text-align:center">
     <h1>Through the Looking Glass</h1>
     <h3>Chapter 7</h3>
     <img src="alice_7_5.jpg" width="306"
     → height="432" border="0">

✓ Tips

  • Unlike the other methods described in this chapter for inserting external content, PHP will not degrade gracefully if the external file being referenced does not exist.

  • Another distinct advantage of PHP is that since the action takes place on the server, you don’t have to worry about browser inconsistencies or incompatibilities.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint