What is the difference between PHP require and include?

后端 未结 7 907
时光说笑
时光说笑 2020-11-30 12:35

I know the basic usage of PHP require, require once, include and include once. But I am confused about when I should use them.

Example: I have 3 files, eg: settings.

相关标签:
7条回答
  • 2020-11-30 12:48
    • require
      when the file is required by your application, e.g. an important message template or a file containing configuration variables without which the app would break.

    • require_once
      when the file contains content that would produce an error on subsequent inclusion, e.g. function important() { /* important code */} is definitely needed in your application but since functions cannot be redeclared should not be included again.

    • include when the file is not required and application flow should continue when not found, e.g.
      great for templates referencing variables from the current scope or something

    • include_once
      optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead

    0 讨论(0)
  • 2020-11-30 12:53

    require_once would check if the file is already included and not include it again, so don't worry it won't load settings.php twice.

    0 讨论(0)
  • 2020-11-30 12:59

    Difference:-

    include() Fetch data and load contain in current file and also load same file more than one time.

    include_once() work same as include() but we use include_once() if a file has already been included, it will not be included again. if use same file as multiple time in Like:- include_once 'setting.php'; use second time include_once 'settting.php'; ignore them.

    require() work same as include().

    require_once() if file has already included, it will not be included again.

    include() and include_once() produce warning and script will continue.

    require() and require_once() produce fatel error and stop the script.

    Better to use

    require_once() in your script.

    0 讨论(0)
  • 2020-11-30 13:05
    • include includes a file and throws a warning if the file was not found.

    • require includes a file and throws a fatal error if the file was not found.

    • include_once and require_once do the same thing, but only if the file was not already loaded.

    However, the need for one of the _once functions is usually a sign of bad design. You should build your scripts in a way that clearly defines what gets included where.

    Choose one place for settings.php to get included - probably index.php. There should be no need to additionally include it in database.php.

    0 讨论(0)
  • 2020-11-30 13:08

    You don't load settings.php two times, as per PHP documentation on 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.

    0 讨论(0)
  • 2020-11-30 13:08

    if your file is important, I think you should use "require",
    but if not, i always use include, like peka's answered
    require includes a file and throws a fatal error if the file was not found.

    0 讨论(0)
提交回复
热议问题