Yours | Mine | Web | Tutorials | Resources


PHP Includes

Want to know the most helpful trick in coding any layout!?

PHP Includes! Once you use them, you'll never not use them again :)

First off, you want to take your code and separate it into three parts: header, body, and footer. Here's an example of a very basic page:

<html>
<head>
<title>
The Title of my page
</title>
</head>
<body>
<font color="pink">
This is The content of my page. Also called the body.
</font>
</body>
</html>

The header would be anything coming before your main content or body. Basically, anything you want to keep the same on each page of your site. I would keep the <font> tag as part of the header because as part of my layout, I'd want each page to have the same font color.

<html>
<head>
<title>
The Title of my page
</title>
</head>
<body>
<font color="pink">

The body would include all of the content that is unique to each page.

"This is The content of my page. Also called the body."

The footer would be anything coming after your main content or body. This is the second half of your layout that is the same on every page.

</font>
</body>
</html>

Now paste your header, body, and footer into three separate notepad documents. Save the header as header.txt, the footer as footer.txt and the body as whatever you want that unique page to be called but make it a .php extension. ex: index.php. *

You can close your header and footer documents now, you don't need to edit those any further.

Go back to your body document. Add this tag to the top, before any of the other content:

<? include ("header.txt"); ?>

And add this to the bottom, after all of the content:

<? include ("footer.txt"); ?>

Upload all three documents to your server. If done correctly, your site should look exactly the same as it did before. Now...go to the top of your internet browser and click 'View' and go to 'Source'. You should not see any of the PHP code, but your header and footer should be inserted there. That's how PHP works. Even in the source code, you can't even tell it's there. :)

Make sure you add the include codes to the top and bottom of EVERY page. You should then be able to just change your header and footer to change your layout, instead of changing it on each page separately. Very handy!

* Is your file saving as index.php.txt instead of just index.php? If you are using notepad to write your documents, make sure you pull down the drop-down menu when the window to save pops up, and change it to 'All File Types'. That way you'll be saving it as a .php instead of .php.txt.

Free Web Hosting