What is the difference between PHP require and include?

后端 未结 7 935
时光说笑
时光说笑 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

提交回复
热议问题