PHP Learning notes-the difference between include, require, include_once, and require_once

Source: Internet
Author: User
Tags variable scope

1.include: The specified file will be read into and executed inside the program.
The program code in the file being imported is executed, and these programs are executed with the same variable range (variable scope) as the source file where the call to the include () function is located. You can import static files from the same server, and you can even use the include () and fopen () functions to import files on other servers.

2.include_once: The role and the include () are almost identical
The only difference is that the include_once () function checks that the file to be imported is already imported elsewhere in the program, and if it does not re-import the file again (this feature is sometimes important, such as the file you want to import declares some functions that you define yourself , then if the file is repeatedly imported in the same program, an error message will occur at the second import, because PHP does not allow the same name function to be repeated for the second time.

3.require: The contents of the target file will be read into and replaced by the contents of these read-in.
This read-and-replace action occurs when the PHP engine compiles your program code, not when the PHP engine starts to execute the compiled program code (the PHP 3.0 engine works by compiling one line, but PHP 4.0 has changed, and PHP 4.0 is the first to put the entire After all the program code is compiled, the compiled program code is executed once, and no program code is executed during the compilation process. require () is typically used to import static content, while the include () is suitable for importing dynamic program code.

4.require_once: Like the include_once () function, the require_once () function will first check that the contents of the target file have been imported before, and if so, it will not re-import the same content again.

Tips:1, usually using the Require () function to import copyright announcements (copyrights), static text or other program code that does not contain variables, or that itself needs to rely on other executed programs to execute correctly.

2, at the beginning of the program usually use the Include () function to import some function library or similar program code.

3, the use of include () and require () to the site often need to change the shared content to do a reasonable division, in the update site content will be easier to do much.

4, require () and include () in addition to how to deal with the failure in all aspects are exactly the same. The include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing a page when you lose a file, don't hesitate to use require (). The include () is not the case and the script will continue to run.

PHP Learning notes-the difference between include, require, include_once, and require_once

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.