Working with External Files

Working with external files is a fairly common thing for a web developer to do. Let's look at some examples of why you would want to be able to read an external file.

Imagine Updating a Web Site

How often do you go to a web page and notice that the same elements on the top, sides, and bottom stay the same? Almost all websites have this repeating pattern. Ever wonder how long it takes to update all of those pages?

Well the answer is "not long"...if it's done smartly that is.

While content management systems are often used in large sites, they still rely on the process of importing external files to simplify the process.

Importing an External File with PHP

Using PHP we have four (4) separate commands that we can use to import an external file. While all are similar, they do behave slightly differently.

Examples of Importing a File

Example #1

Let's import a saved guest book entries for your web site. The guest book is stored in an external text file.

<html>
<head><title>My Guest Book</title></head>
<body>
....
<?php
echo '<h2>My Guestbook</h2>';
require('guestbook.txt');
?>
...
</body>
</html>

See the example here. The text file here.

Now let's examine the code to see what is happening.

We'll ignore the boiler plate html/head/title/body tags - this was covered in the previous topics.

The PHP Tag itself

<?php - This is the start of the PHP tag. If your file has a "php" extension - for example guestbook.php, then the server will see the php tag, and run as a script everything between the start of the PHP tag, and its end. You cannot see what happens inside the php tag unless you programmatically tell it to display.

?> - Is the ending php tag. After this, the server just sends the HTML to the browser, until either the end of the file, or it encounters another <?php opening tag.

The PHP commands

Did you notice the semi-colon (;)?

This lets PHP know that we are at the end of one command, and ready for a new command. Leaving it out is a common mistake (even among those who do it fairly often).

echo '<h2>My Guestbook</h2>'; - The echo command is the most common way to display HTML within our PHP tags. This generates whatever HTML we want to show. It can be as simple as just a few characters, or entire pages of information.

require('guestbook.txt'); - This imports the external file, and displays it if applicable.

Did you notice the single quotes?

This lets PHP know that we are giving it a string. A string is a sequence of characters, that does not have to be calculated. PHP allows you to define a string by surrounding the characters with either a set of single quotes, or a set of double quotes.

Example #2

Another common example is when we need to include those common elements which are on every page. This would be things like your header, menu, and footer sections of the page. Consider the following example.

<?php include('template/header.htm'); ?>
<?php
echo '<h2>My Guestbook</h2>';
require('guestbook.txt');
?>
<?php include('template/footer.htm'); ?>

See the completed file, the header file, and the footer file.

In this example we include multiple files. Let us look at these files and see what is happening.

The PHP Commands

<?php include('template/header.htm'); ?> - A couple of differences from our last file. First, notice that the PHP tags and the command are all on the same line. This is completely legal. My personal preference is that if I have only one PHP command, I write it this way, on one line. If I have multiple PHP commands, I write the opening and closing parts of tag on their own lines, with each command on its own line as well. This is a personal style and preference.

Did you notice the sub-folder called "template"?

A common technique used to organize files more clearly is to place files defining how they look into a template, or similarly named folder.

<?php include('template/footer.htm'); ?> - This works the same way as the header, it just is calling a different file.