PHP Include

A common practice is to define a set of functions for an application and place them in a PHP file which is loaded by all other scripts in the system. External PHP files can be loaded using the include() function:


There are four functions in PHP to include an external file:

  • include()
  • include_once()
  • require()
  • require_once()

The difference between include and require is that the first will always attempt to continue processing the scripts, while the latter generates a fatal error in case of issues (e.g. cannot find the required file). You can use include_once() and require_once() to make sure the external file is not included more than once.

Here an example about how to include a file with useful functions:



Any PHP code in the file being included must occur inside <?php and ?> tags. Normal text and HTML in the file will be output straight to the browser. This makes the include() function extremely useful for keeping repeated elements of web sites (such as the common navigation or header) in a single file.

Here an example about how to use include() to reproduce a template:



STEP 1: split the template into several php files to be included:








STEP 2: include the external php files: