If you have ever worked with Servier Side Includes (SSI), you are already familiar with the concept of include files. If you haven't, it is a simple and elegant method of adding code to your page.

Consider a snippet of code that you need to repeat again somewhere else, on another page perhaps. A good example would be the footer at the bottom of each of these pages. You could write a function that echos the code, then call the function wherever you wish to insert the code. When the code is complex or lengthy, this can be cumbersome, but it would work.

A less cumbersome way to do the task above is to write all the code just as you would want it to appear on the page, then save it in a separate file, which you insert as needed. This separate file is called an 'include' file.

Here is all the code used to produce a footer at the bottom of each of these pages:

<hr style="clear:both" /> <center> <table border="0" cellpadding="5" cellspacing="0" style="font-size:x-small; margin:0; width:100%;"> <tr> <td align="left" width="120px"> <!-- HTML-Kit logo --> <a href=""><img src="htmlkit2.gif" alt="Developed with HTML-Kit" width="90px" height="30px" border="0" style="padding-left:10px" /></a> <form action="#" method="post" target="_top" style="margin-top:5px"> <input type="submit" name="logout" value="New Session" /> </form> </td> <td align="left" width="250px" style="text-align:left;"> <!-- webmaster email --> <a href="" style="font-size:small;" target="_self"> Sandersongs Web Tutorials</a><br /> Contact the <a href=" Site Comments">Webmaster</a> with comments.<br /> ©<?= date("Y"); ?>, by Bill Sanders, all rights reserved.<br /> <span style="color:maroon">This site had <?=$visitorNo;?> different visits in the last 30 days.<br /> <?php $query1 = "select hit from hits"; $link = mysql_connect($host,$user,$pwd) or die("Could not connect to Host."); mysql_select_db($db) or die("Could not select database."); $query = stripslashes($query1); $result = mysql_query($query) or die("Could not run query."); $answer1 = mysql_fetch_array($result1); $newNum = $answer1[0] + 1; echo number_format($newNum); $query2 = "update hits set hit=$newNum"; $query = stripslashes($query2); $result2 = mysql_query($query) or die("Could not run query."); $answer2 = mysql_fetch_array($result2); ?> hits on this domain since 24 Nov 2006.</span> </td> <td align="right"> <div class="lastmod"> <!-- last updated and URL --> <?php echo 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"].$_SERVER["QUERY_STRING"]; ?><br> This page was last modified on our server on <?php echo date("j M Y",filemtime($_SERVER["PATH_TRANSLATED"])); ?><br> and last refreshed on our server at <?php echo date("g:i a, T"); ?><br> <script language="JavaScript" type="text/javascript"> <!-- document.write('Which reads as '+timeStr+' on your computer.<br />'); // --> </script> This file took <?= endTimer(); ?> seconds to process. </div> </td> </tr> </table> </center>

The PHP interpreter will assume whatever file is included is written in HTML. Thus, you have to add <?php ... ?> where needed.

Now, if the code ever needs to be changed, it can be changed in one place, the include file, and all of the web pages will be correct.

You can name your include files whatever you like. I like to use a *.inc extension. This helps identify the files as not being full web pages (as a *.php file might suggest) and can be easily grouped.

To include a file anywhere on your page, use the following syntax (within PHP): <?php include(""); ?> where '' is whatever you named the file. In this example, '' is in the same directory as the file calling it. If not in the same directory, you need to provide the path to the file. is now inserted just as if you wrote it there in the first place.

There are four include functions that do pretty much the same thing: include, which was discussed above, include_once, require and require_once. Include is identical to require except that if include fails, it produces a warning (the script continues), and if require fails, it stops the program. Include_once and require_once are similar to their namesakes, but they can only be called once in the running of a script. This comes in handy if you need to avoid redefining functions or overwriting variables.

Include and require behave differently in conditionals. Require will operate whether it is in a conditional statement or not. Consider the following:

<?php // will not be included if $variable != '' if ($variable=='') {include('');} // will be included regardless of the value of $variable if ($variable=='') {require('');} ?>

Test Query   < <  PREVIOUS   Table of Contents NEXT  > >   Forms

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2019, by Bill Sanders, all rights reserved.
This domain had 5,080 different visits in the last 30 days.
This page was last modified on our server on 4 Jun 2008
and last refreshed on our server at 11:26 am, MST
This file took 0.01088 seconds to process.