Basic file includes

Probably one of the most useful aspects of PHP is the include. This is the simplest use of it (PHP) too, and will save you so much time on updating and uploading new updates for your web sites.

Why is it handy?

A usual web site has generic items on every page. Let’s say the footer on every page is the same, something like:

<div id="footer">© My Company Ltd, 1999 - 2005.</div>

A typical footer sitting at the bottom of every page. Now imagine what happens next year when you want to change the date! It’s okay on a site with maybe 5 or 6 pages, but take a web site with 20+ pages. Yes I know, templates in Dreamweaver can mean having to just change the template and voila, all the pages are updated. But now imagine being back on dialup and having to upload 20+ pages. Before the days of PHP I had to update 400 product pages and upload them all on dialup. Didn’t get much else done that day!

By using a file include you can store this little titbit of code in another file and just include it on every page you need it displayed on. Then when you need to update the date you change one file, and upload one file.

How to do it

You strip out the generic code and store it in a page of it’s own. It doesn’t need anything else, just the code you’ve stripped from the main page. I personally use a “.inc.php” extension to let me see at a glance which the include files are. i.e.
<div id="footer">© My Company Ltd, 1999 - 2005.</div>

Then in your main pages that require the footer you just have the following:

(XHTML Code)
<?php include ""; ?>
(XHTML Code)

There are no limits to how many includes you can have in your web page. You can even use includes within includes for more complex pages which I’ll go into later.

You may also like...

6 Responses

  1. pest says:


    I just started using xhtml and basic includes (about three months ago)

    except i’ve been using (for example) “”,
    rather than – “”

    is this a problem?

  2. Sarah says:

    Good point. I said what to do but not why. A .inc file will be parsed by a browser as plain text. This is fine if that .inc file doesn’t contain any important information. But when you come to start placing in sensitive information, which can range from the top most ie. usernames and passwords through to even the knowledge of variable names, then someone could use the information they can see and compromise your site or even the server you’re on.

    By using .inc.php you’re telling yourself it’s an include file but you’re telling the browsers to parse it as PHP, therefore any PHP would be executed, not displayed for all to see.

    For example, if you have

    // - store mysql connection details
    $username = "dbuser";
    $password = "dbpass";

    a simple include with your mysql username and password in for use throughout the site for database connections. Well if I could view that as plain text then I’d know your username and password.

    So ideally I tend to use .php for PHP pages, .inc.php for includes, .fnc.php for functions and .scr.php for pure script. Just makes it easier to find files on very large sites.

  3. pest says:

    phew! was worried I had some re-building to do there!

    still I guess it’s possible that my includes might contain something important in the future!

    is there any benifit to .inc?

  4. Sarah says:

    Not really. .inc just stands for include. It’s not a recognised file extension as is parsed by browsers as plain text.

    .inc and .inc.php are just used by programmers so they realise that they’re includes.

    When you start on large sites and working with lots of files, it makes understanding a file structure easier.

  5. Marc Eilbeck says:

    Very nice guide to learning php. Its something ive always wanted to learn so I think I will be coming back and reading the whole guide at some point!

  6. Sarah says:

    Well it should give you the basics. I kind of ran out of time after a while but to be honest once the basics make sense a book would most likely be the next step. The SitePoint Learning PHP book is a good choice.