Include Files
Re-useable HTML Structure: When we put together our site files, we wish to retain as much flexibility as possible, while maintaining a single location for making site changes. To accommodate this apparent contradiction, we can use include files to house recurring HTML elements, CSS styles & code. An advantage of using includes is we can remove all structural HTML from our pages.
Not all PHP pages consist of one file. We can 'break up' PHP pages into several files, each file holding a fragment of an HTML page. Like CSS, this has the effect of allowing us to make a single change to one file, and have the change cascade across several pages. The difference is that CSS cascades format changes, and include files cascade HTML (or PHP) across pages!
Files vs. Pages: While we build HTML pages we think of a 'page' as the document that ends with an .html extension. A 'page' however is best thought of as what the user sees, that consists of HTML which can weave together the contents of several different files.
Even in the simplest web pages we use images. The image is not part of the HTML document, but is referenced, and called by the browser, when it sees the reference in the HTML document. CSS and JS files are also called when identified in a page in much the same way. Therefore the 'page' has always been the combined effect of combining several files.
Since we'll be including files that are only fragments of HTML pages, we'll use the word Page to denote a PHP page as a web page file that is designed to be navigated (viewed) by a browser. Pages can reference additional included files. That way we can talk about a page including files without confusing the two.
Basic Document Structure: On a typical web page, just as in a word processing document, we frequently find a header and a footer. The header & footer areas provide the context for the page.
When using include files in PHP, we'll build on the theme that pages are created of multiple files. Each of the files could contain a fragment (piece) of the HTML page. The most obvious reason to do this is to add a 'header' or 'footer' to our pages.
In many pages, a header or footer identify the website, and place copyright and contact information into several pages. Consider a large website, with dozens to hundreds of pages. What if change was requested to the copyright notice at the bottom of each page? If the pages were built with include files, the change could occur in one file, and that change would 'cascade' across all site pages! The 'footer' file thus involved would only be a fragment of HTML, and not be an entire page.
What goes between the header and footer identifies (makes unique) the specific document. If the top and bottom of a document are the header and footer, what goes between can be called the guts. When we work with a page, this is the area we would like to concentrate on.
Page Identity: When using include files we'll identify parts of our pages that deserve to be consistent across all pages. Each page however, still retains it's purpose, which we'll call it's identity. The page identity implies a single file to which all of the include files are a part.
This is the traditional 'center' of the page, sometimes called the 'guts' in a design (in relation the words header & footer) is what distinguishes any page from all others.
For example, a 'links' page features different information from an 'about us' page.You'll hear me use the word 'identity' to define this area, and in fact the entire page. The purpose of a page should be clearly defined, and that purpose gives the page an identity. All visible elements of the page should contribute to this identity.
PHP Functions: Many programming tasks have been built for us and exist as built in functions inside the PHP language. When you see the name of a function linked in our pages, I usually link to PHP.NET, which is where I recommend you look first for answers about PHP!
PHP include function: include is a core PHP function. You may include any number of other files on a page, by including them, at the appropriate place in the page:
include("my_inc.php");
I use a convention of placing the letters inc at the end of the filename to identify the file is a fragment and not a navigable web page. Besides header and footer HTML, include files can contain functions we create or even navigational elements that change dynamically based on the page in which they are included.
Here is an alternate structure for including a file. This version loses the parens and uses a single space between the string that is the name of the file:
include 'my_inc.php';
The above example assumes the include file is in the same folder as the page that includes the file. We can use relative pathing, just like we do frequently with images:
include 'include/styles.css';
Later we'll use constants to store absolute paths that we can change when we move our applications between different hosts:
include VIRTUAL_PATH . 'include/styles.css';
Deconstructing an HTML Page: Once we have a solid HTML design which works in all browsers, we'll save this, and study it to break into PHP include files.
When we decide how much to include in our header/footer files, we find out that it pays to include as much as possible inside these files. The more code that remains inside the the includes, the less coding is required to make site wide changes.
Placing all structural HTML inside the header & footer allows us to go from div to table based design (or vice versa) as needed. It also allows us to more easily accommodate other devices, as the header & footers can be swapped out dynamically inside the include files.
Here is a typical HTML page, before we deconstruct it into include files:
We can study the HTML of this page and isolate the specific parts of the page that may make good include files. A good target for this would be pieces of the page that may be consistent across many pages (not necessarily all) pages of a site. In our case, we elect to isolate a left "Nav", a "header" (a banner identifying the site) a "footer" (an identifying element including copyright info on the bottom of the page) and a left "Nav" area which may exhibit consistent site links. A good approach would be to come up with the site design basics, study carefully the HTML and identify early candidates for include files, which benefit from making changes to HTML in one file, and having it effect all applicable site pages.
Dreamweaver To The Rescue: If we have a complicated HTML page to split up, we can view the file via Dreamweaver. We can then highlight the HTML we wish to split out via design view and then look at the code view to see where to draw the line and split up the file.
In the following example we have deconstructed the HTML page above into include files. Study the code in this example for details on the page:
Include, require & once: When using include files we can use the function include, plus a couple of variations,require, include_once and require_once. Later on we'll see that the 'once' designation is great for making sure file is only included once in a page. This helps minimize some PHP errors.
Speed alert: One way to make PHP files a bit faster is to use include instead of the require and 'once' variants. Due to a peculiarity of PHP, it can make the page a bit faster to load as PHP does not need to search for other files before loading the one you are requesting.