Cleaning up: including files
So far, each script you've written has been self-contained, with all of the code in a single PHP file. In this section, you'll look at organizing your code into multiple files. You'll take sections of code that you use on multiple pages and place them into a separate file, which you'll then include in the original pages.
PHP provides two ways to include files. One is for including support files, such as interface elements, and the other is for crucial files, such as functions called within the page.
Start by creating the files you'll eventually include. Whenever you create a website, one of the first things you need to do is create a header and footer file that contains the major interface elements. That way, you can build as many pages as you want without worrying about what the page looks like until the coding work is done. At that point, you can create the interface just once, in the include files, and the entire site will be instantly updated.
To start, create a file called top.txt and add the code in Listing 43.
Listing 43. Creating a file called top.txt
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Workflow Manager</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="wrapper"><div id="bg"><div id="header"></div>
<div id="page"><div id="container"><div id="banner"></div>
<div id="nav1">
<ul style='float: left'>
<li><a href="#" shape="rect">Home</a></li>
<li><a href="#" shape="rect">Upload</a></li>
<li><a href="#" shape="rect">Files</a></li>
</ul>
</div>
<div id="content">
<div id="center"> |
In a separate file called bottom.txt, add the following shown in Listing 44.
Listing 44. bottom.txt
</div>
</div>
</div>
</div></div></div>
</body>
</html> |
Save both files in the same directory as registration.php. Copy the images directory and the style.css file from the PHPPart1SampleFiles.zip in Downloads to that directory as well, as they're referenced by the HTML code in top.txt.
Now go ahead and add these files to the registration page. Edit registration.php to look like Listing 45.
Listing 45. Adding top.txt and bottom.txt to the registration page
<?php
include("top.txt");
?>
<h1>Register for an Account:</h1>
<form action="registration_action.php" method="POST">
Username: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Password: <input type="password" name="pword[]" /><br />
Password (again): <input type="password" name="pword[]" /><br />
<input type="submit" value="GO" />
</form>
<?php
include("bottom.txt");
?> |
Notice that you've removed the HTML that normally surrounds the content of the page and replaced it with a command to include the files you just created. Now it's time to see what that action does.
If you now point your browser back to the registration page, you will see a much different look, as shown in Figure 15.
Figure 15. New look of registration page
If you do a "view source" on the page, you can see that all three files have now been merged in the output (see Listing 46).
Listing 46. Files have been merged in the output
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Workflow Manager</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="wrapper"><div id="bg"><div id="header"></div>
<div id="page"><div id="container"><div id="banner"></div>
<div id="nav1">
<ul style='float: left'>
<li><a href="#" shape="rect">Home</a></li>
<li><a href="#" shape="rect">Upload</a></li>
<li><a href="#" shape="rect">Files</a></li>
</ul>
</div>
<div id="content">
<div id="center">
<h1>Register for an Account:</h1>
<form action="registration_action.php" method="POST">
Username: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Password: <input type="password" name="pword[]" /><br />
Password (again): <input type="password" name="pword[]" /><br />
<input type="submit" value="GO" />
</form>
</div>
</div>
</div>
</div></div></div>
</body>
</html> |
If you go ahead and make the same changes to registration_action.php and submit the form, you'll see that the changes take place immediately.
Now, this page isn't a work of art, and that's OK. Later, you can get a designer to make it look nice, and you'll have to make the changes only once—to the included files—rather than to every page on the site.
If PHP can't find interface files, it's a problem,
but it isn't necessarily a catastrophe, especially if all
you're worried about is the functionality of the application.
As a result, if PHP can't find a file specified by
the include()
function, it displays a warning and continues processing the page.
In some cases, however, not being able to find an include file is a catastrophe.
For example, you can pull the validate()
script out into a separate file and include that file
in the registration_action.php file.
If PHP can't find it, that's a problem because you're calling that functions within the page.
So, to avoid that, you can use the require() function, instead of
include() (see Listing 47).
Listing 47. Using the
require() function
<?php
include("top.txt");
require("scripts.txt");
?>
<p>You entered:</p>
<?php
foreach ($_POST as $key=>$value) {
echo "<p>".$key." = " . $value . "</p>";
}
$passwords = $_POST["pword"];
echo "First password = ".$passwords[0];
echo "<br />";
echo "Second password = ".$passwords[1];
if (validate($_POST) == "OK") {
echo "<p>Thank you for registering!</p>";
... |
If PHP can't find a page that's required, it sends a fatal error and stops processing.
There's nothing to stop you from including a file in a file that is itself included in another file.
In fact,
with all of these include files floating around, it can get confusing, and you may inadvertently
include the same
file more than once. This duplication can result in interface elements appearing multiple times, or
errors due to
the redefinition of functions or constants. To avoid that, PHP provides special versions of the
include()
and require() functions.
For example, you can be sure that the registration_action.php file will load
the files only once (see Listing 48).
Listing 48. Ensuring that the registration_action.php file will load the files only once
<?php
include_once("top.txt");
require_once("scripts.txt");
?>
<p>You entered:</p>
<?php
... |
When PHP encounters the
include_once()
or
require_once()
function, it checks to see
if the file has already been included in the page before including it again.




