PHP include() Vs require()

PHP control structure comprises several types of statements. In this article, we are going to compare the include() and require() statements and their _once usage. These statements are used to add the script from external file.


So the PHP files are embedded into one another in various ways based on the following PHP functions.

  1. include()
  2. require()
  3. include_once()
  4. require_once()

For all the above functions, we need to specify the file path to be embedded. This path might be either of an absolute path or of a relative path. Let us see about the functions one by one in the following the discussion.


This function is used to include an external file into a PHP file. The syntax of include statement as follows.

include("../file_name.php"); // relative path
include("/xampp/hddocs/file_name.php"); // absolute path

We should any one of the above statements. Because more than one attempt at adding include statement will return an error like as follows if the target file has the code for a function declaration.

Fatal error: Cannot redeclare...

If the target file does not exist, then the warning notice will be created as,

Warning: failed to open stream: No such file or directory...
Warning: Failed opening ... for inclusion...


This function will perform in a strict manner where it won’t allows the program for further execution if the requited target file does not exist. Instead of warning notice created by the include(), this will generate an error like as shown below.

Warning: failed to open stream: No such file or directory...
Fatal error: Failed opening required...

Syntax of this statement is,

require("../file_name.php"); // relative path
require("/xampp/hddocs/file_name.php"); // absolute path

This function also won’t allow the multiple attempts of adding require statement for the same file as like as include() function.

include_once() and require_once():

Both are similar to the include() and require(), respectively. The only difference is that these statements won’t return any error message on attempting to include the same file more than once. Rather, the _once usage will check, if the target file is included already. If not, the file will be included, otherwise, the execution will skip to next statement. And the syntax of these statements is shown as below.

include_once("../file_name.php"); // relative path
require_once("../file_name.php"); // relative path

In older versions of PHP, include() was used to embed external file conditionally where as require() will refer target file as a resource.

This PHP code tutorial was published on April 22, 2013.

↑ Back to Top