Last updated on May 12th, 2022 at 01:27 pm

There are four core constructs for including files into your PHP scripts. The main objective is for you to create code in separate files and then be able to use that code to include functions, variables and etc, in other PHP scripts. You have two main options.

To include() a file or to require() a file. We get into the specifics in a moment and you will quickly understand what the differences are.

The include() Construct

The include() construct is the most commonly used method to include files amongst developers. Its purpose is to simply include a file. If the file does not exist, it will return a warning and still allow the script to operate even if the warning is issued.

Example:

<?php
include($_SERVER['DOCUMENT_ROOT'].'/get.php');
?>

Now all of the code, and functions from get.php will be available throughout the rest of the current PHP script for use with the rest of your code.

If the get.php file doesn’t exist or cannot be included then it will throw similar warning

PHP Warning:  include(/get.php): failed to open stream: No such file or directory in /var/www/inc.php on line 2

The include_once() Construct

Ok, the main difference between the include_once() construct and the include() construct is that if the file has already been included in this code execution, it will not be included again. This is a good method to use and I would recommend using the standard include_once() construct because it can prevent you from redeclaring functions that you may have already included previously. As your code becomes more complex, you may have files included in different PHP files and when calling those files, you may start running into problems.

Example:

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'get.php');
?>

If the get.php file doesn’t exist or cannot be included then it will throw similar warning

PHP Warning:  include_once(get.php): failed to open stream: No such file or directory in /var/www/inc.php on line 3

The require() Construct

The require() construct is the same as include, but one major difference. If the file does not exist, or cannot be included, a Fatal Error will be produced and the execution of the PHP script will be halted! This construct is important for applications you develop that has dependancies from other files for example database connection settings, in order for your script to function properly.

Example:

<?php
require($_SERVER['DOCUMENT_ROOT'].'/get.php');
?>

Error if file doesn’t exist

PHP Fatal error:  require(): Failed opening required 'get.php' (include_path='.:/usr/share/php') in /var/www/inc.php on line 2

The require_once() Construct

This construct is the one that I use more than the other three. Personally, I feel that this construct takes into account all of the necessary reasons you would be including a file in the first place. Just like include_once() the require_once() construct determines if the file has already been included and if it has been, it will skip this instance. In addition, a Fatal Error will be produced just like the require() construct does if the file cannot be read or included.

Example:

<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/get.php');
?>

Leave a Reply

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