Web Statistics

include and require

You've got to quit lowering your standards.
Set your requirements up front so when a guy hooks you,
he has to know this is business. - Steve Harvey

There will come a time when you want to have another file, or multiple files, included in the execution of a script. Say for instance that you have several classes in your program. Each class is contained in its own file. Let assume that you are executing the file order.php, but it needs access to the objects in the classes Customer (located in customer.php) and the class Products (in products.php). Continuing this example lets walk through how to accomplish this. The basic structure we will have is that the order.php file is located in a directory called app, and all the class files are found in a subdirectory of app call classes.

				/app
  |-- order.php
  |-- /classes
       |-- customer.php
       |-- products.php
			

include / include_once

include

The following in a condensed version of the contents of what the order.php file might contain.

				$customer = new Customer();
$products = new Products();
...
$customer_info = $customer->getInfo($customer_id);
$product_list = $products->getProducts($order_id);
			

As you can see the Customer and Products are not found in the file, but are referenced. This would result in a Fatal error message thrown. To add the Customer and Products class to the scope of the script we need to include the class files like so:

				include 'classes/customer.php';
include 'classes/products.php';

$customer = new Customer();
$products = new Products();
...
$customer_info = $customer->getInfo($customer_id);
$product_list = $products->getProducts($order_id);
			

When including a file PHP will look up the file in the following order:

  1. The path given in the include.
  2. The include_path specified.
  3. In the directory the executed script is located

The include construct will emit a warning (E_WARNING) if it cannot find a file.

include_once

The include_once control operates just like the include control except that if the a file has already been included, it will not be included again.

				include_once 'classes/customer.php';
include_once 'classes/products.php';

$customer = new Customer();
$products = new Products();
...
$customer_info = $customer->getInfo($customer_id);
$product_list = $products->getProducts($order_id);
			

require / require_once

The require and require_once controls are just like the include and include_once controls, respectively, with the exception that if the file cannot be found, or if it cannot be accessed due to permissions, the process will throw a Fatal error (E_COMPILE_ERROR). This is because, as the name suggest, the inclusion of the file(s) is required and not optional.

				require 'classes/customer.php';
require 'classes/products.php';

$customer = new Customer();
$products = new Products();
...
$customer_info = $customer->getInfo($customer_id);
$product_list = $products->getProducts($order_id);
			

Up Next

Learn to express yourself...or search for them at least