The PHP INCLUDE Function

 

Being able to include other files into your HTML code, or for your PHP scripts, is a useful thing. The include( ) function allows you do this.

Suppose you have a text file that you want to include in a web page that you've already got up and running. You could copy and paste the text from the file straight into you HTML. Or you could use the include( ) function

As an example for you to try, there are two files amongst the ones you downloaded (in the scripts folder), called include.php and textfile.txt. Load up the one called include.php.

Now take a look at the code for this page:

<HTML>
<HEAD>

<TITLE>Include files</TITLE>

</HEAD>

<BODY>

<H3>Normal text here </H3>

Normal text written in a HTML Editor

<H3>Include File here</H3>

<?PHP include "textfile.txt" ; ?>

</BODY>

</HTML>

Our PHP code is in red. Here it is:

<?PHP

include "textfile.txt";

?>

So in between PHP script tags, type the word include. After the word include, type the name of the file you want to include on your page. Your filename can either go after a space, and between quotation marks, or you can put it in round brackets (again, with the quotes).

As well as including text, you can include HTML. This can save you lots of work. For example, a web page typically contains a menu bar, with links to other areas of your site. Something like this:

A HTML menu bar

Suppose you decide to add a new section to your site. The new page should be like this:

A new menu item added with a PHP include

If your site contains lots of pages, that would mean having to amend the HTML of all of them. A painful and dreaded task! Instead, use the include( ) function.

To see how it works, load up the page called links.php that is among the files you downloaded (in the scripts folder): you should see the first menu bar. This has the include line, that points to another file - linksPage.txt (this is also in the scripts folder).

If you open up the text file called linksPage.txt, you'll see that it's just a HTML table. To get this table into the PHP page called links.php, we just did this:

<?PHP include "linksPage.txt" ?>

The point is, if we had the include line on all pages of out site, and we had to add a new section, we could just change the text file linksPage.txt. This change would then mean that all the pages in the site would be updated!

Try it yourself. Add the following line to the page called linksPage.txt. Put it between the TABLE tags:

<TR>

<TD height="30" valign="middle" bgcolor="#FFFFCC">

<a href="links.php">New Section</a>

</TD>

</TR>

Save the page, and then load up links.php again. You should see a new section added to your menu bar.

 

Including Scripts

You can also use the include( ) function for scripts. You could include those valuable error checking functions that you've stored in one PHP file. Or just use it to cut down on the amount of code in the page.

As an example, load up the page called includeScript.php (in the scripts folder that you downloaded). The code is quite simple. It's just this:

<?PHP

include "myOtherScript.php";

print "This was printed from the includeScript.php";
print "<BR>";

doPrint();

?>

The above script uses include to include another PHP script - myOtherScript.php (also in the scripts folder). The function called doPrint() is in myOtherScript.php. If you open that file, you'll see it's just this:

<?PHP

function doPrint() {

print "This was printed from the myOtherScript.php";

}

?>

Load up the page called includeScript.php in your browser. You should see two lines printed out.


So, include is a very useful function – one of the most useful inbuilt PHP functions available to you!

 

In the next section, we'll look at some of the security issues with the code you've written so far. It's only a short section, but it's essential reading!