Those of us in the web development business are more than familiar with PHP. In fact, most of us are crazy about it (except for those of us stuck using ASP).
But maybe you haven't used it yet because you aren't a programmer.
Even if that's the case, there is still plenty of PHP that you could
be using. If you do any sort of web design and would be
willing to learn to use a single PHP function, make it
include().
What is an include function?
If you don't have a programming background, the concept of an "include" function may not make any sense. Different programmers and languages may call these different things, but the concept is the same.
When you "include" a file, it's kind of like taking the contents of a file and putting them inside another. From the perspective of a web designer (rather than a programmer), it's analogous to using a FRAME or IFRAME to put another file inside an HTML file.
In fact, you can even use PHP's include() to do some
of the same things you may be doing with FRAME and IFRAME. More on
that in a bit.
How PHP includes are useful to web designers
One of the classic issues of web design is finding a way to include a navigation list on all of your site's pages. You can do this manually, but if the list changes then you have to go through all your pages and edit them just to add even a single link.
If you have a small site (5 or 10 pages), you probably do this by hand. As you get more complicated, you may use an editor like Dreamweaver to create templates that will automatically update specific areas of all your pages. Unfortunately, many developers are working in a lot of different locations and don't always have such software available.
So if you get tired of the editing, you might cave and start using FRAME and IFRAME to include your navigation list. This isn't the best choice many times because there are a lot of cases where these HTML tags are not such a great idea; if nothing else, they tend to be ugly and are at the mercy of the web browser.
But if you have PHP available on your web server, this problem becomes trivial to solve. All you need to do is create a file that holds your navigation list (name it menu.html, menu.php, or whatever you want) and then include it in every page you make for that site.
include()'s syntax
To use an include(), you will use code in the
following form.
<?php
include($string);
?>
Even though the string you specify will include a filename and possibly a path, it is a string that you are using as a parameter. I point this out because the first time I did this, I found that odd give my personal background in programming. This is actually a good thing because it means you can use variables to change the file you include, which will be useful if you end up doing more advanced things with PHP.
So in most situations, your include()
will work something like this:
<?php
include("/path/to/your/included/files/somefile.php");
?>
Once that bit of code is placed in your web pages, the contents of somefile.php are evaluated when those pages are opened. If something.php is just a file with HTML code, that means that all your web pages will look like you added the contents that file to every web page's source code when opened in a web browser.
Some notes on paths
If you've done most of your work in plain HTML, you may not be familiar with "paths." The easiest way I know to explain paths is with this analogy: paths are to operating systems as URLs are to web servers.
For example, on your webserver your site's path is probably something like
/home/accountname/public_html
but your URL is more like
http://yourserver.tld/~accountname
If you aren't sure what your account's path is, you should probably ask your web host to find out for sure. Of course, if you learn a little more PHP you'll be able to figure this out on your own.
You may be wondering if you could just use the URL for the file you want to include as the path. In some situations, the answer is yes. But depending on your server's settings, this probably won't do what you want it to do anyway. In other words, if everything isn't in the same directory it's best to either use full paths or traverse directories using "../" within your path.
A sample implementation
You may want to use PHP includes in a way similar to the sample below. First, we will create menu.php. This will be just a short HTML snippet without any HEAD or BODY tags.
<ul>
<li><a
href="page1.php">Page 1</a></li>
<li><a href="page2.php">Page 2</a></li>
<li><a href="page3.php">Page
3</a></li>
<li><a
href="page4.php">Page 4</a></li>
</ul>
Next, we'll edit each of our individual pages to look something like this
<html>
.
.
.
<body>
.
.
.
<div id="nav_menu">
<?php include("menu.php"); ?>
</div>
<div id="main_content">
{main page content}
</div>
.
.
.
</body>
</html>
Once you open such a page in a browser and view the source, it will look like this
<html>
.
.
.
<body>
.
.
.
<div id="nav_menu">
<ul>
<li><a href="page1.php">Page
1</a></li>
<li><a
href="page2.php">Page 2</a></li>
<li><a href="page3.php">Page 3</a></li>
<li><a href="page4.php">Page
4</a></li>
</ul>
</div>
<div id="main_content">
{main page
content}
</div>
.
.
.
</body>
</html>
Can you now see what a useful little
function include() is? By using CSS in addition to PHP,
you can dramatically change the look of your entire site by just
editing a few files.
Conclusion
As I said, if you never learn any other
PHP function, learn include()! This little function is
incredibly powerful when used as a time-saver. If you'd like to see
a little more of it in action, you might want to read my article on
PHP
templating.
It's also a great introduction to PHP that will convince almost any web designer to give the rest of PHP a look. I've often joked that just about anything you'd want to code in PHP probably already has a function in the standard PHP library. Once you start to use PHP even just casually within your HTML code, I'd be willing to bet you'll come to the same conclusion.

Tech Articles
Rollie Hawk is a consultant, web publisher, online personality, magazine writer, web developer, network administrator, teacher, husband and father residing in southern Illinois. He graduated in 2002 from Southern Illinois University, earning his BS majoring in math with a minor in chemistry.