scripting   intro |  html |  css |  design |  scripting |  dhtml |  server 

> PHP Intro
> PHP Config
> PHP Includes
> Quick Install Apache, MySQL, PHP
PHP Guestbook
> Intro
> Project Plan
> Database structure
> PHP Code
> Sign Guestbook

PHP Include files

This page covers:
     Why Use Include Files?
     View Code for Include Files
     How to Use Include Files in PHP
     How to Use Include Files in ASP

Why Use Include Files?

I like modular code. I like making changes to just a few files and having those changes propagated throughout the entire web site without having to edit every single file. Include files help make this happen.

For example, every page at has a separate header and footer file included. The header file includes the start of the HTML page and opens the table I put all my content into. The footer file closes the table and contains the footer links if any. This way I don't have to edit every single page whenever a link changes, or when I want to change the look of the site - I can simply edit the header.php file in each of the directories and make my changes there. There is a different header.php file in each directory because the left hand navigation changes depending on what section you're in. The footer files remain pretty constant throughout the site, though I can customize the footers as well.

I also include a file called format.php in every header.php file. The format.php file includes definitions of my most commonly used variables - for example, I define all my fonts, my paragraph tags, base href, and other variales that will make writing my content files just that much easier. Using variables in my code lets me change them easily. For, my default font face is "Arial, Helvetica, sans-serif". If I decide that I don't like that font - I can simply change the "" variable in my format file and it will effect this change across the entire web site. One format.php file is used for the entire site.

I also keep my functions in separate include file(s) so that pages that don't need that code included can be kept free from the extra kb.

View Code for Include Files

  • This file contains all my common variables and is included on all pages.
  • This is the template file that controls the look of the site. Each php file includes this template.

How to Use Include Files in PHP

The PHP configuration file lets you set up an auto_prepend and auto_append files (header & footer files) which automatically get included on every file. The only downfall to this is that sometimes you may not want to include a header and footer file. If you have the auto_prepend and auto_append feature turned on, and you do not have the appropriate files in the directory you're trying to view a file in, it will not serve the file out. For example, say you want to view index.php in and the php3.ini file is configured to auto prepend/append header.php/footer.php and these two files are missing from the /temp directory, you will get a fatal error when it fails to open the files it needs to include.

If you use the auto_prepend and auto_append feature, you do not need to add any additional code to your pages. The PHP engine will automatically add those files. You can also choose to take advantage of the include_path. You can set this to your default include directory (say /includes) and when you add the include tag to your php pages, it will look first in the directory that particular page resides in to see if there is a file by the name of the include file and if not, will include the file by that name in the default include path you've configured.

For example, if you have a default file that you want to include in many of your files, say a company information file with name, address, phone number, you can call this "co_info.php" and put it in your default includes directory. But in the Store directory of your company's website, you want to post additional or different company info so you created a different "co_info.php" file and put it in your Store directory. On all your php pages, you'll include the tag, <? include("co_info.php") ?>. When a visitor comes to that page, PHP will check to see if there is a "co_info.php" page in its current directory and if there isn't one, it will include the "co_info.php" from the default include directory.

The PHP code to include files is: <? include("filename.php") ?>

How to Use Include Files in ASP

There are two ways to include files in ASP. With the "include file" or "include virtual" options. "Include virtual" tells ASP where the include file is relative to the web root directory, "include file" tells ASP where the file is relative to the current directory. Use either of the following tags to include files in ASP:

<!-- #include virtual="/includes/header.asp" -->
(tells ASP where the file is located relative to the root web directory)

<!-- #include file="../includes/header.asp" -->
(tells ASP where the file is relative to its current directory)

NOTE: Do not put any spaces between the open bracket (<), exclamation mark (!), or the 2 dashes (--). If you put ASP code in the include files, make sure that the code is enlosed in opening & closing ASP brackets ( <% %> ). The include file tags should be placed outside of the opening and closing ASP tags on the calling file. Click here to view sample code.

nav id: & sect id:

Next Page: Quick Step Guide to Installing Apache, MySQL, PHP
Previous Page: The PHP configuration file © 1999-2011. all rights reserved. // site created and maintained by kathy ahn