Will require_once and include_once include the file together twice?

If I have this:

require_once('myfile.php'); 

and then:

 include_once('myfile.php'); 

Will the file be included again or only once?

What is the difference between the two? Does the request cause an error on failure and include recovery attempts? any other differences?

+6
include php
source share
3 answers

If the file is included, it is included.

requice_once() works exactly the same as include_once() , except that it kills the script when the script to include is not found.

Therefore, in your example, the script is included once.

+11
source share

Conceptually: Include does what it says in tin: it contains the code "myfile.php" in the code of the current file. The requirement is more like an import statement in other programming languages.

The suffix _once means that PHP avoids inlcuding / require file if it is already available for the current script through the previous include / require statement or recursively through include / require in other imported scripts.

0
source share

Will the file be included again or only once?

myfile.php simply included once.

 require_once 'myfile.php'; //included include_once 'myfile.php'; // not included 

What is the difference between the two?

The difference between include_once and require is that when they cannot find the include_once file, a warning is issued and require will produce a fatal error.

require is identical to inclusion, except in cases of failure, it will also lead to a fatal error of level E_COMPILE_ERROR. In other words, it will stop the script, while it includes only a warning (E_WARNING), which allows the script to continue. [ http://php.net/manual/en/function.require.php]

Does the request cause an error on failure and include recovery attempts? any other differences?

include allows you to recover yes, fail always ends in one way, try catch cant even save the script.

It is worth noting what will happen if other combinations of the four file rating evaluations ( include , include_once , require and require ).

If the file was found

include and require will always include a file regardless of whether you have previously used require , include , require or include_once with the same file.

 include_once 'myfile.php'; //included include 'myfile.php'; //always included require 'myfile.php'; //always included require_once 'myfile.php'; // not included, prevously included via [function.include_once], [function.include] and [function.require] 

include_once and require will only include the file if it was not previously included using require , include , require or include_once .

 require 'myfile.php'; //always included include_once 'myfile.php'; // not included, prevously included via [function.require] 
0
source share

All Articles