Differences between include () and require () in PHP

Source: Internet
Author: User
There are two methods to reference files: require and include. The two methods provide different elasticity. RequireFor example require("MyRequireFile.php");. This function is usually placed at the beginning of the PHP program. before the PHP program is executed, it will first read the file specified by require to make it a part of the PHP program webpage. This method can also be used to introduce common functions into webpages.

IncludeFor exampleinclude("MyIncludeFile.php");. This function is generally placed in the process of process control. The PHP program webpage reads the include file. In this way, you can simplify the process during program execution.

The two of them have the same purpose, and they do not have to put at the beginning or in the middle. The most fundamental difference between them is that the error handling methods are different.

If an error exists in a file in require, the program will be interrupted and a fatal error will be displayed.
If an include file has an error, the program will not be in the middle, but will continue to execute and display a warning error.

The following is a supplement:

1. include has a return value, but require does not.

2. include () includes and runs the specified file. when the processing fails, include () generates a warning that the imported program code will be executed, in addition, these programs have the same variable range as those called to the include () statement in the source file during execution. You can import static pages on the same server.

3. the role of include_once () is almost the same as that of include ().
The only difference is that include_once () will first check whether the file to be imported has been imported elsewhere in the program, if yes, it will not be repeated (this function is sometimes very important. for example, some of the functions you have defined in the file to be imported, if the file is repeatedly imported in the same program, an error message will occur during the second import, because PHP does not allow the function with the same name to be repeated for the second time ).

4. require () will read the content of the target file, and replace itself with the read content. in case of processing failure, require () will cause a fatal error.
This read-and substitute action occurs when the PHP engine compiles your program code, rather than when the PHP engine starts to execute compiled program code (the PHP 3.0 engine works by compiling a line to execute a line, but it changes after PHP 4.0, PHP 4.0 is to compile the entire program code first, and then execute the compiled program code once. during the compilation process, no program code will be executed ). Require () is usually used to import static content, while include () is suitable for importing dynamic program code.

5. like include_once (), require_once () checks whether the content of the target file has been imported before. If yes, the same content will not be imported again.

5. require is unconditionally included, that is, if require is added to a process, require will be executed no matter whether the condition is true or not.

7. require is usually placed at the beginning of the PHP program. before executing the PHP program, it will first read the file specified by require to make it a part of the PHP program webpage. This method can also be used to introduce common functions into webpages.

8. include is generally placed in the processing part of the process control. The PHP program webpage reads the include file before reading it. This method simplifies the process during program execution.

Difference between require (), include (), require_once () and include_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.