Comparison of include, include_once, require, and require_once in PHP

Source: Internet
Author: User
Comparison of include, include_once, require, and require_once in PHP

[Objective] Comparison of include, include_once, require, and require_once in PHP


[Function used] include, include_once, require, require_once,

Dirname (_ FILE _) obtains the absolute path of the current FILE. compared with the relative path, the search speed is the fastest.


① The include and require statements are used to insert useful code into other files in the execution stream.

Include is very similar to require, except for the differences in error handling:

  • Require generates a fatal error (E_COMPILE_ERROR) and stops the script.
  • Include will only generate warning (E_WARNING), the script will continue
  • Therefore, if you want to continue the execution and output the results to the user, use include even if the included files are lost. Otherwise, in the framework, CMS, or complex PHP application programming, always use require to reference key files to the execution stream. This helps improve application security and integrity when a key file is accidentally lost.


    ② The execution speed of the four functions is almost the same.


    The difference is that if a file is loaded multiple times, the include and require functions are loaded multiple times, while the include_once and require_once functions are loaded only once. Processing load failures at the same time is different. include () and include_once () generate a warning while require () and require_once () lead to a fatal error.

    (1) the include_once () statement contains and runs the specified file during script execution. This behavior is similar to the include () statement. The only difference is that include_once () first checks whether the file has been included. if it already exists, ignore this inclusion.

    Include_once () should be used for nested inclusion. to ensure that it is only included once, the function definition and variable re-assignment should be avoided.


    (2) the require_once () statement contains and runs the specified file during script execution. This behavior is similar to the require () statement. The only difference is that require_once () first checks whether the file has been included before. if it already exists, ignore this inclusion.

    ③ Precautions

    (1) path problem: especially when nested inclusion, you must pay attention to the path containing the file.

    For example, if file A contains file B, file B contains file C, and the ABC file is not in the same folder, it is prone to errors.

    Solution:

    You can use the dirname (_ FILE _) statement to obtain the absolute path of the current script.

    Example: include (dirname (_ FILE _). "/inc/conn. inc. php ");

    Dirname (_ FILE _); obtain the directory name dirname (_ FILE _) at the top of the FILE.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.