Home > Article > Backend Development > The difference between include() and require() in php
There are two ways to reference files: require and include. The two methods provide different usage flexibility.
require is used like <span style="font-family:NSimsun">require("MyRequireFile.php");</span>
. This function is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.
include Usage methods such as <span style="font-family:NSimsun">include("MyIncludeFile.php");</span>
. This function is generally placed in the processing part of Process Control. The PHP program web page only reads the include file when it reads it. In this way, the process of program execution can be simplified.
The two of them have exactly the same purpose, and it does not necessarily have to be which one is placed at the front and which one is placed in the middle. The most fundamental difference between them lies in the different ways error handling.
require If there is an error in a file, the program will interrupt execution and display a fatal error
include If there is an error in a file, the program will not end, but will continue to execute and display a Warning error.
The following are supplements:
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. The imported program code will be executed, and these programs will have and call the source file when executed. to the same variable scope as the position of the include() statement. You can import static pages from the same server.
3. include_once() has almost the same function as include()
The only difference is that include_once() will first check whether the file to be imported is already in the program If it has been imported elsewhere in the file, it will not be imported again (this function is sometimes very important. For example, if the file to be imported declares some functions that you have defined yourself, then if it is imported at the same time, it will not be imported again. If a program imports this file repeatedly, an error message will occur the second time it is imported, because PHP does not allow functions with the same name to be declared twice).
4. require() will read the contents of the target file and replace itself with the read contents. If the processing fails, require() will cause a fatal error.
This reading and substitution action occurs when the PHP engine compiles your program code, not when the PHP engine starts executing the compiled program code (the way the PHP 3.0 engine works is to compile a line Execute one line, but this has changed since PHP 4.0. PHP 4.0 first compiles the entire program code, and then executes the compiled program code at once. No program code will be executed during the compilation process. ). require() is usually used to import static content, while include() is suitable for importing dynamic program code.
5. Like include_once(), require_once() will first check whether the content of the target file has been imported before. If so, it will not import the same thing again. Content.
5. require is an unconditional inclusion, that is, if require is added to a process, require will be executed first regardless of whether the condition is true or not.
7. require is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.
8. Include is generally placed in the processing part of the process control. The PHP program webpage only reads the included file when it reads it. This method can simplify the process of program execution.
The difference between require(), include(), require_once() and include_once()
The above is the detailed content of The difference between include() and require() in php. For more information, please follow other related articles on the PHP Chinese website!