Why include when you can require in PHP?

What is the point of trying to use the include or include_once library in PHP if you often have to use require or require_once instead (since they are usually critical)?

+45
php
May 17 '11 at 5:35
source share
9 answers

The difference between include / include_once and require / require_once is that when the file is not found, the former triggers a warning, while the latter throws an error. Opinions may differ, but IMO, if the required file is missing or readable for any reason, this is a very difficult situation, and I would rather see the error and stop execution than to warn and continue execution in an obviously broken context. Therefore, I believe that it is best to use require / require_once and avoid include / include_once in general.

+31
May 17 '11 at 5:42
source share

Sometimes you include files from libraries that you do not control, or your code runs in different environments.

Perhaps some of them are not really required for your script, but are auxiliary files or additional functions.

For example, if your PHP generates HTML, you can use include to add the banner. But you will not want your file to stop if for some reason this banner file was missing. You would gracefully fail.

+45
May 17 '11 at 5:47
source share

enable is an attempt to download a file but does not stop execution if it fails

include_once - does the same as include, but does an additional check to ensure that the file has not yet been included in the current script

required - this is an attempt to download a file and gives a fatal error on error

require_once is the same as require, but does an extra check to make sure it is loaded only once

If you do not need to use include_once or require_once, try to avoid them, as additional checking adds a bit of overhead.

EDIT: I have to add that the key difference is that at the compiler level a crash is required and at runtime it includes an error.

+7
May 17 '11 at 5:42
source share

The request will be to stop the script if it cannot include the file.
Include will display a message .

Btw: * _ runs slowly once . You should not use it unless you really need to.

+3
May 17 '11 at 5:38
source share

Because you do not want the missing files to stop your show .;)

+2
May 17 '11 at 5:38
source share

Obviously includes will generate a warning ...

So, if you have potentially missing files (for example, dynamically including templates that can be safe for failure), and you suppress warnings (or don't mind bloated logs), then file_exists and require just becomes include .

However, this does not seem to be a very common use case. The reason it includes is still very common ( Occam razor ?), Most likely simple:

  • include makes more sense and is displayed in other languages.
+2
May 17 '11 at 5:45
source share

All four can accept a local file or URL as input. require() and include() functions are almost identical in function, except for how they handle an irrevocable resource. include() and include_once() gives a warning if the resource cannot be extracted, and try to continue the program (if possible), while the require() and require_once functions stop processing the page if they cannot find the resource.

It is best to use require_once() to include files containing code and include_once() , to include files that do not contain code, for example. HTML, CSS, etc. (This is my approach, which may differ.)

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

+2
May 17 '11 at 5:47
source share

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 is for query ().

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

require () is identical to include () except in cases of failure, create a fatal E_COMPILE_ERROR error level. In other words, it will stop the script, while include () throws a warning (E_WARNING), which allows the script to continue.

See the include () documentation for how it works.

I think enough :)

0
May 17 '11 at 6:33 am
source share

The difference is only in error handling, when the required file does not exist: in the case of include you can easily process the warning, change the execution to show the usual "Sorry, error" page, send a notification to the site administrator.
In the case of require your script will simply be stopped, the fatal error will be more difficult to handle, and you will not be able to show the user any message.

0
May 17 '11 at 7:13
source share



All Articles