简体   繁体   中英

Why include when you can require in PHP?

什么是试图点includeinclude_once PHP中的库,如果你经常使用requirerequire_once ,而不是(因为它们通常非常重要)?

Sometimes you are including files from libraries that you do not control or your code is being run on different environments.

It is possible that some of these aren't actually required for your script but are helper files or extras.

For instance, if your PHP is generating HTML, you might use an include to add a banner. But you wouldn't want your file to halt if for some reason that banner file was missing. You'd want to gracefully fail instead.

The difference between include / include_once and require / require_once is that when a file is not found, the former triggers a warning whereas the latter triggers an error. Opinions may vary but IMO, if a needed file is not present or readable for some reason, this represents a very broken situation and I would rather see an error and halt execution, than have a warning and proceed execution in a clearly broken context. So I believe it to be best practice to use require / require_once exclusively and avoid include / include_once altogether.

include - this attempts to load a file but does not halt execution if it fails

include_once - does the same as 'include' but runs an additional check to make sure the file hasn't already been included in the current script

require - this attempts to load a file and throws a fatal error on failure

require_once - same as 'require' but runs an additional check to make sure it's loaded only once

If you don't need to use include_once or require_once try to avoid them since the extra check adds a little overhead.

EDIT: I should add that they key difference is that require fails at the compiler level, while include throws an error at run-time.

Require will stop the script if it can't include the file.
Include will just give an error message.

Btw.: *_once is slow. Shouldn't use it unless you really need to.

Because you don't want the missing files stop your show. ;)

Obviously, include will only generate a warning ...

So, if you have potentially missing files (such as dynamically including templates that may be safe to fail) and you are suppressing warnings (or don't mind bloated logs) then file_exists & require simply becomes include .

However, that doesn't seem like a very common use case. The reason that include is still very common ( Occam's Razor ?) is more likely, simply this:

  • include makes more semantic sense, and is seen in other languages.

All four can take a local file or URL as input. require() and include() functions are virtually similar in their function except for the way they handle an irretrievable resource. include() and include_once() gives warning if the resource cannot be retrieved and try to continue execution of the program(if possible) while require() and require_once functions stops processing the page if they cannot find the resource.

It is best to use require_once() to include files which contains code and include_once() to include files that do not contains code eg HTML, CSS, etc. (This is my approach other's can differ.)

Also read this: http://arin.me/blog/php-require-vs-include-vs-require_once-vs-include_once-performance-test

The documentation at http://tr.php.net/manual/en/function.include.php starts with:

The include() statement includes and evaluates the specified file.

The documentation below also applies to require().

And the documentation at http://tr.php.net/manual/en/function.require.php says:

require() is identical to include() except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.

See the include() documentation for how this works.

I think that's enough :)

Difference just in error handling, when required file doesn't exists: in case of include you can easily handle warning, change execution to show normal 'Sorry, error' page, send notification to site's admin.
In case of require your script will just stopped, fatal error is more difficult to handle, and you can't show any message to user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM