[web-tips > includes]

The wonderful world of "includes" by tedd

The most important technique I found in doing web development over the last 18 years has been knowing how to use the "include()" statement. If you can grasp how to use php-includes, then it will simplify your coding considerably.

For those who don't know what an include() is, or those who think they do, but don't use it -- then I say if you follow and understand what I provide here, you will change the way you work forever! It's that important for you to know what the include() statement is and how to use it.

First, you don't have to be a programming wizard to use it -- it's very simple. The statement simply allows you to include redundant code.

For example, let's say you have a menu that spans several web pages -- instead of repeating that code in every page, you create a menu file (menu.php) and then "include" it like so:

						
<?php include('menu.php'); ?>
 

You place the above statement where your menu code would have been and automagically the include will be loaded when your web page is loaded. That's mondo cool.

This makes it great for sections of code that are repeated in other pages, like the header, menu, and footer. You keep/edit/maintain just one file regardless of how many times it may be needed elsewhere.

Most of my pages begin and end like so:

	
<?php include('includes/header.php'); ?>

... html...

<?php include('includes/footer.php'); ?>
 

My header.php file has all the stuff from the DOCTYPE to the start body tag AND has it's own include to include the menu. So includes can include includes.

To read more, you can review this:

PHP Includes

The only real difference between before/after you do this is that you must have all your files end with ".php". I can provide a file that will change the way the php interpreter will look at files, but it's easier to just change the suffix. Besides, that's what all the wizard programmers do anyway.

If you want an example of code to review -- please note the below two sites look exactly alike, but they are coded considerably different:

Example without Includes

Example with Includes

The code for both sites can be found as follows:

Code for Example without Includes

Code for Example with Includes

One parting note, these examples also show a reason why one should switch from using image roll-overs to css roll-overs -- it's much easier to change things, plus it loads quicker.

In any event, if you don't currently use the include() statement, then studying these two examples will alter the way you code forever.

tedd


Comments
tedd   Thursday, February 18, 2010 08:03
 
matthew:

Answer to your first question,namely: 1) Where specifically am I to put the helper code from part 2? In the head or body of the code?

You may put it anywhere you want. I recommend that you place it at the bottom of the page, as I recommended in the instructions. You see, it doesn't make any difference where the code is located because the code is run and html is created before the browser see's anything.

Answer to your second question is a bit more complicated because you are using a "server-side" javascript include, which is not necessary. If you want to include something, just include it this way:



If you want to learn to learn something that will rock your web-design world, then learn how to use php includes -- as demonstrated here:

http://sperling.com/examples/include-demo/

If you learn that, you'll never need another javascript routine to do something that should have been done pre-browser. Don't get me wrong javascript has it's place, but it's place is client-side and if you can do something server-side, then do it there. That practice is better than expecting the user to have javascript always turned on.

Cheers,

tedd
matthew   Monday, February 22, 2010 15:13
 
2 questions:

1) Is the syntax for an include:

[angle-bracket]?php
or
[angle-bracket]php ----(no "?")

2) Do all of my files on the server have to be of file type php? I have made them like that, and firefox tries to download the php file. it will not display anything.
tedd   Wednesday, March 10, 2010 08:20
 
matthew:

Good catch -- the first example should be "<?php" and not "<php" -- I fixed the page.

As for the second question, I read somewhere that all web pages should identify what they are at the beginning of their text as well as with their suffix. As such, sometimes browsers like FireFox (a great browsers btw) has problems with some files if they are not properly identified. So, one can use something like this --

<?php header("Content-Type text/css; charset=UTF-8);?>

-- to proceed a css file, for example.

However, FireFox would never download a php file for a php file is executed on the server long before the browser see's the output. As such, the browser should NOT be downloading php files. If it is, then there is something very wrong.

tedd
Del Wegener   Monday, March 21, 2011 12:05
 
Thanks Tedd;
A few years ago, I read some comments you made on CSS forum or WD about
includes and I have never developed the same since that time.

When you do the page header as an include, how do you get the correct title, and keywords? Do you break the header into two parts which skip those two lines and put them in manually for each page?

Del

"Post a Comment" has been deactivated.

I'm sorry, but there is just too much spam to review to provide answers. For example, for every honest question, there are over 1000 spams. I am leaving the comments here so that they may help others.

I find my time better spent providing more examples and attending to clients.

tedd