PHP

require VS include & require_once VS include_once in PHP

include()

The include() function includes and evaluates the file during the execution of the script. Files are included based on the file path given or the include_path. If the file isn’t found include() will check in the calling script’s own directory and the current working directory before failing. include() will emit a warning (E_WARNING) if it cannot find a file.

For more information about include() function check PHP manual.

require()

The documentation above also applies to require(). require() will emit a fatal error (E_COMPILE_ERROR) if it cannot find a file.

include_once()

The include_once() function includes and evaluates the specified file during the execution of the script. If the code from a file has already been included, it will not be included again, it will be included just once.

For more for information about include_once() function check PHP manual.

require_once()

The require_once() function will check if the file has already been included, and if so, not require it again.

Tips

If you would like to include files which contain necessary source code for the application, use require_once or require.

If you would like to include files which contain content that the program can run without them, use include_once or include().

Functions include() and include_once() if they fail, the script will still continue executing.

Functions require() and require_once() if they fail, the script will stop.

Examples

include 'somefile.php';
include 'somefile.php' // somefile.php is included again
echo 'hi'; //if somefile.php does not exist, the application prints hi

include_once 'somefile.php';
include_once 'somefile.php' // somefile.php is NOT included again
echo 'hi'; //if somefile.php does not exist, the application prints hi

require 'somefile.php';
require 'somefile.php' // somefile.php is included again
echo 'hi'; //if somefile.php does not exist, the application DOES NOT print anything, 
//the application has stopped in the first line of code

require_once 'somefile.php';
require_once 'somefile.php' // somefile.php is NOT included again
echo 'hi'; //if somefile.php does not exist, the application DOES NOT print anything, 
//the application has stopped in the first line of code