Almost all online scripts and applications will feature a core library of variables and functions that get utilized throughout the script. For example, let’s say you’re writing a database intensive application. You could declare your MySQL details within each page of your script – however if you ever changed your password that would mean that you would need to update every single page of your script with the new MySQL details. Rather than declaring the password in each of your page scripts, you can store that name and password in a separate file. You can then include that file as a part of your script, and whatever variables you declare in that file will be declared in your script! Furthermore, you can store long scripts or functions in a separate file and include them only when you need them. For example, if you are writing an eCommerce shopping cart script the function that gets real-time UPS shipping quotes is 24KB worth of XML processing goodness, but you use it only when someone chooses UPS as a shipping option. Why not store it in ups_ship_quotes.php and call it only when necessary?

In fact, almost all heavy-duty PHP applications have a file called something like config.php, which declares the critical variables that every page needs to know, such as the MySQL name and password. Those same applications also store frequently used scripts in different directories. Programmers then mix and match, taking the check-to-see-if-a-user-is-logged-in script from one directory, including the get-the-relevant-data-from-the-database script from another directory, and writing a central script that screens the data based on whether the user is logged in or not.

So let’s dig into how we can do the same.

require_once("/path/to/file.php");

The file that you pass through to require_once() is now a part of your script, exactly as if you had cut and copied the contents of the file into your script. You can even include HTML files to create a simple templating system.

PHP will interpret any file you pass through as PHP code – so make sure you have your opening and closing php tags (<?php ?>) around the PHP code in your included file, otherwise PHP will simply print the entire file.

If PHP cannot locate the specified file the script will die and produce a fatal error. If you wish to let the script continue to run even if the specified file isn’t found simply replace require_once with include_once, like so:

include_once("/path/to/file.php");

We can take this a step further by combining the require_once() statement with other control structures. Let’s go back to the example mentioned above where we discuss including the UPS shipping quotes script only when the UPS shipping option is selected and take a look at how we can accomplish that.

if ($shippingOption == "UPS") {
require_once("ups_ship_quotes.php");
}

That’s all there is to this. Pretty simple right? Now there are a few things to take note of here.

  • First off always make sure that you have specified the correct path for your included file otherwise the script will return back a fatal error stating it cannot find the specified file.
  • Secondly make sure that the file you are including is not located inside a restricted directory. Certain hosting companies will have restricted certain directories from PHP access for security purposes.
  • If you plan to utilize cookies or PHP sessions within your site make sure that the files you include have no whitespace outside the PHP tags to avoid any header_output already sent errors.

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Leave a comment

Your email address will not be published. Required fields are marked *