Repeating Content Across Pages

One of the first things new users of PHP will do is use PHP to repeat content over multiple pages. There are a few ways to do this.

The include Language Construct

PHP has many built-in functions that are powerful and ready-to-use, some of which are called "language constructs" (I won't go into why, for now). One of these is include();. include(); takes one argument (which is sort of like a variable), the name of the file that PHP is to include. So, let's say we had a copyright statement, which we might store in a file called copyright.php.

Contents of copyright.php:

<div id="footer"> <p>All content copyright 2000-2006.</p> </div>

Note that we've called this file copyright.php, even though it's really just a fragment of HTML code. We do this in case we later want to add some PHP code, which we will in a moment.

To use this code, we'd embed in our Web pages, at the exact spot we wanted it to appear, this little fragment of PHP code (assuming that copyright.php was either in the same directory as the page including it, or in the directory of our PHP include path):

<div id="content"> <p>...I hope you enjoy my site.</p> </div> <?php include 'copyright.php'; ?> </body> </html>

Alternatively, especially if you can't configure your own PHP include paths, you might put a folder in your site root called includes, and place copyright.php in it. Then, you can confidently refer to that include file from anywhere by changing the PHP include code to:

<?php include '/includes/copyright.php'; ?>

Now, every page will draw its copyright statement from the copyright.php file. So if suddenly we wanted to change the text to "The material on this page is copyright 2000-2006. All rights reserved," we wouldn't have to change every file on our site. Likewise, when the year rolls over from 2006 to 2007, we can just edit the one file, and 2007 would appear everywhere.

Includes with Dynamic Content

Of course, PHP has a handy function called date();, which can automatically pull any date information we'd like, and format it (the PHP documentation site has a complete list of all date formatting options). This way, every January 1 we don't have to remember to change the year on our site; we let PHP do it for us by passing the argument 'Y' to date, which is a numeric representation of the year in 4 digits. So now, we change up our copyright.php file to look like this:

<div id="footer"> <p>All content copyright 2000-<?php print date('Y');?>.</p> </div>

Now, assuming the time is correct on the server we're using, the correct, most recent year will always appear in the footer.

Advanced Content Repetition

While include does get the job done, it's somewhat limited, because it can only dump content from the external file it calls wherever include appears.

Suppose, for example, we wanted to repeat the entire <head> portions of our XHTML documents across entire pages. For some elements, like the XHTML DOCTYPE declaration, include might work fine. But what about other <head> elements like <title>?

Here's a basic document header that we might use:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Document Title Goes Here</title> </head>

Now, that's all fine—until we get to the document <title>, which should be unique for each page. The solution? We'll start a new include file, and just call it sitefunctions.php, and begin by creating our very own function, which we'll call xhtml_opener(). We declare a new function by typing "function", and then the function's unique name (which is up to us, though be careful you're not duplicating the name of a function already used in PHP!); xhtml_opener will take the argument $pagetitle/

sitefunctions.php <?php function xhtml_opener($pagetitle) { print <<<END <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>$pagetitle</title> </head> END; } ?>

Now, in the above example, we're using another built-in PHP function called print();, and we're using it with a special syntax called "here document" syntax, which allows us to run quotation marks (") without escaping them (normally, in print statements, quotation marks are escaped with a backslash, like this: \") and to include PHP variables, like $pagetitle, without concatenating them (we'll look at that later).

So in the PHP file of our actual site (we'll call it index.php), we'd need to do two things. First, we include the sitefunctions.php file, and second, we call the xhtml_opener function when we need it, e.g.:

index.php <?php include 'sitefunctions.php'; xhtml_opener('Welcome to Karl\'s Home Page'); ?> <body> <p>Hi, welcome to my page...</p> </body> </html>