Inside a PHP file you can include other PHP files. We have the following methods, all used for this use case, but slightly different: include, include_once, require, require_once.

include loads the content of another PHP file, using a relative path.

require does the same, but if there’s any error doing so, the program halts. include will only generate a warning.

You can decide to use one or another depending on your use case. If you want your program to exit if it can’t import the file, use require.

include_once and require_once do the same thing as their corresponding functions without _once, but they make sure the file is included/required only once during the execution of the program.

This is useful for example if you have multiple files loading some other file, and you typically want to avoid loading that more than once.

My rule of thumb is to never use include or require because you might load the same file 2 times, include_once and require_once help you avoid this problem.

Use include_once when you want to conditionally load a file, for example “load this file instead of that”, and in all other cases, use require_once.

Here’s an example:

require_once('test.php');

//and variables defined in the test.php file


The above syntax includes the test.php file from the current folder the file where this code is in.

You can use relative paths

require_once('../test.php');


to include a file in the parent folder or to go in a subfolder

require_once('test/test.php');


You can use absolute paths:

require_once('/var/www/test/file.php');


In modern PHP codebases that use a framework, files are generally loaded automatically so you’ll have less need to use the above functions.