The difference between include () and require () in PHP

Source: Internet
Author: User

Go to the script house http://www.jb51.net/article/22467.htm

The use of require is as follows require("MyRequireFile.php"); . This function is usually placed at the front of the PHP program, and before the PHP program executes, it is read into the file specified by require to make it a part of the PHP program's Web page. Commonly used functions, you can also use this method to introduce it into the Web page.

Include usage methods such asinclude("MyIncludeFile.php");。 This function is usually placed in the processing part of the process control. The PHP Program page reads the include file before it is read in. This way, you can simplify the process when the program executes.

The two of them are used exactly the same, not necessarily which one is in the front and the other in the middle. Their most fundamental difference is that they are not handled in the same way.

Require a file is wrong, then the program will break execution and display a fatal error
Include a file if there is an error, then the program will not be the middle end, but continue to execute, and display a warning error.

The following supplements are:

1. Include has a return value, and require does not.

2. Include () includes and runs the specified file when processing fails the include () generates a warning that the imported program code will be executed and that the program will execute with the same range of variables as the location of the call to the include () statement in the source file. You can import static pages from the same server.

3. The role of Include_once () and the include () are almost identical
The only difference is that include_once () will first check if the file to be imported has been imported elsewhere in the program, and if it does not repeat the import again (this feature is sometimes important, for example, to import a declaration of some functions that you define yourself, If you import this file repeatedly in the same program, an error message will occur on the second import because PHP does not allow the same name function to be repeated for the second time.

4. require () will read the contents of the target file and replace itself with these read-in content require () causes a fatal error when processing fails.
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 a line, but changes after PHP 4.0, PHP 4.0 is the entire program code to complete the compilation, and then the compiled program code once executed, in the process of compiling will not execute any program code). Require () is typically used to import static content, while the include () is suitable for importing dynamic program code.

5. Like include_once (), require_once () 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.

5. Require is an unconditional inclusion, that is, if a process joins require, the require will be executed first, whether or not the condition is established.

7. Require is usually placed in the front of the PHP program, the PHP program before execution, will be read into the require specified by the introduction of the file, making it a part of the PHP program page. Commonly used functions, you can also use this method to introduce it into the Web page.

8. Include is generally placed in the Process Control section of the PHP program when the page is read to the include file, it will be read in. This way you can simplify the process of executing the program.

The difference between include () and require () in PHP

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.