PHP includes question [duplicate]
Possible Duplicate:
When should I use require_once vs include?
which php function is better to use include
,include_once
, require
, require_once
and are there any other functions that are similar to these functions?
And what are the pros and cons of each function?
It depends on what you need to do.
Do you need to ensure it is included only once? Use the *_once
construct.
Do you want an error thrown if the file does not exist? Use the require*
construct.
Note that these are not functions, but language constructs.
They should only be used to include other PHP files. For opening any other type of file, use something else (such as readfile()
).
Note that if using classes and __autoload()
or spl_autoload_register()
(preferred), you don't have to include the class, it is included implictly.
Include just includes it when it sees that directive, regardless if its already included it. This can sometimes cause conflicts if you are including the document in multiple locations.
Include_once does just that, it ensures that it includes the file only once.
Require is just like include, although it also ASSERTS this condition and will kill your app if it can't find the required file.
Require once is just like include_once, except is also ASSERTS this condition, killing your app if the file isn't found.
As the function names suggest:
- include will simply try to include the referenced file inside of your file.
- include_once will include the file only if it has not already been included in your script or a script that yours has been included in
- require will thrown an error and exit the script if the referenced file does not exist, thus "requiring" it for execution.
- require_once will do the same thing as require, except only if the file has not yet been included in the current script or a parent script.
Use always *require and require_once;*
Blockquote
because it shows error and stops scripting then you can find the error
精彩评论