Server Side Includes
By Peter McNulty : 22-12-2004
In the general programming world, is it often expected for a good programmer to separate his program into many sections or blocks to enable code re-use and easy maintenance of the scripts. Using server side includes we can produce more modular scripts, as we are able to include pieces of code that have been previously written.
Using a feature that PHP offers, use we are allowed to do these server side includes to include both HTML and PHP pages, and actually any pages we really want to. Including HTML pages becomes very useful, in creating the same look and feel over your entire site, as we can include a standard header and footer on every page.
There are in fact, four different types of includes, all surprisingly similar. They are; include(), require(), include_once() and require_once().
The function include does exactly as it suggests, it simply includes the requested file into your PHP, and if it doesn't find it, it gives a minor error. Here's an example on how to include:
Above, is two separate files, header.php and index.php, where the include syntax is shown. All we do is specify the file to be included as the argument of include(). In our example, header.php is included at the top of index.php to give a page similar to this:
You can see how it can be very useful in creating the same look across your entire site.
Using require() we can do the exact same thing, however, if PHP cannot find the file, then it will end the scripts. Whereas, if we only use include() PHP will only report a minor error. So if we change index.php to this:
Then we can see that it is now trying to include a file that does not exist, consequently we will get an error similar to the following.
It may not be so obvious, but if require() did not halt all execution of the script upon failure, we would get some errors, but the last line would still display. Hence, we would have Here is some main content still displaying. Using require() this does not happen, and so is very useful when including important files, such as configuration or library files.
Include and Require once
The two last include type functions represent the other versions on the functions, however, with these (include_once() and require_once()) they only try to include the file once. So, if the file has been previously included/required (it doesn't matter which), then using these function will not include them again.
I commonly use this method in my class library files, as some of the libraries require other files, some don't. When I write the scripts that include some of these libraries, I may not include of the needed libraries, hence require_once() will handle it for me. But more usefully, if I include a few library files, and one of the library files includes on of the others, it will not get included a second time when require_once() is used.
I keep mentioning the use of require_once() as it the optimal function to use in most cases. This is because of the 'require' nature in that it causes a fatal error if it can't find the file (which is almost always absolutely necessary), and because I don't want to include the same file more than once.
Using these functions I've explained, you can now start creating more modular scripts, by separating useful functions and code into separate files, so that it can be re-used in many scripts. It will also prevent you from copying and pasting the same code into many files.
Page 1 of 1