WordPress include()/include_once() and require()/require_once()

Include Once And Require Once

The difference between include vs. include_once and  require_once vs. require is obvious from the names. include and require allows you to insert a file multiple times within a single executions lifetime. While, include_once() and require_once() make sure one file is inserted only once in a single execution lifetime. The “include” statement is used to include other files into a PHP file. The “require” statement is used to include file.

As the name suggests, with include or include_once, you simply include other files in the flow of execution. On the other hand, when you uses the require or require_once, you are not only include the file in the flow of execution, but also you tell PHP that the execution cannot be continued without that file.

include()

include the following basic syntax:

include()

The include() function will allow to include the file multiple times. includes will only produce a warning (E_WARNING) and the script will continue.

For example,

include(‘header.php’);

include_once()

include_once has the following basic syntax:

include_once()

In the case of the include_once(), if the file is missing, then a fatal error will arise but at least the output will be shown from the file.

For example,

include_once(‘header.php’);

require()

require has the following basic syntax:

require()

The require() function will allow to include the file multiple times. It will produce a fatal error (E_COMPILE_ERROR) and stop the script.

For example,

require(‘header.php’);

require_once()

require_once has the following basic syntax:

require_once()

The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again. In the case of the require_once(), if the file PHP file is missing, then a fatal error will arise and no output is shown and the execution halts. require_once() statement used to include a php file in another one, when you need to include the called file more than once.

For example,

require_once(‘header.php’);