Blinding Light: Free Web Layouts, Anime Transparent PNG, PS Brushes, Coding Tutorials and more!

Blinding Light: Web Layouts, PS Brushes, Coding Tutorials, Transparent PNG

PHP Includes

As some of you might know, in static HTML pages, every time you change a layout, you have to change each and every one of the coding for those pages. This tutorial teaches you how to make changing the layouts easier by simply editing 2 text files.

Step 1: Your Host

Before anything else, you should know what platform your host is running on. If your host is on Linux (Apache Web Server) or you've confirmed that your host supports PHP, you are good to go so read on...

Step 2: Static HTML

Of course we should have the layout. Let's start with something simple...

 1:  <html>
 2:  <head>
 3:  <title>PHP Includes</title>
 4:  </head>
 5:
 6:  <body>
 7:
 8:  <p>Hello, world!</p>
 9:
10:  </body>
11:  </html>

Above is a simple HTML page. The numbers on the side represents the number of lines it has. Of course we leave those numbers out when we make the HTML page.

Next is we divide that page into 3 parts. Line 8 will be the content, lines 1-6 will be the header and lines 10-11 will be the footer.

Step 3: Text Files

What does this all mean? Content, header, footer?!? I'm getting there! Create a new text file in your Notepad and copy and paste lines 1-6 and save it as header.txt. Create another text file which contains lines 10-11 and save it as footer.txt.

So header.txt looks like this:

<html>
<head>
<title>PHP Includes</title>
</head>
  
<body>

And footer.txt looks like this:

</body>
</html>

Simple enough, isn't it? It doesn't have to be txt files. You can save it as an HTML file or PHP file. But for the purpose of this tutorial, we're using txt files.

Step 4: Putting It All Together

Last part of the tutorial! We will now use the include function of PHP. Let's name this file index.php.

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

<p>Hello, world!</p>

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

Yes. That is all the code. No kidding. Told you it was easy. Remember the file extension is .php for it to work. Now, every time you want to change your layout, you just have to edit header.txt and footer.txt.

Conclusion

Don't you just love PHP? The include function is probably one of the most useful. Once you learn it, you just can't code your site without it. You may download the files in this tutorial here. Enjoy!

If you have any questions or if you want to discuss this tutorial, you may post at Blinding Light MB.

Web site design, scripting and contents © 2003-08 Miko Reznor.
No part of this site may be republished without permission.