The difference between include and require

The usage of require is as follows  require("MyRequireFile.php"); . This function is usually placed at the top of the PHP program. Before the PHP program is executed, it will first read 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 eg  include("MyIncludeFile.php"); . This function is generally placed in the processing part of the flow control. The web page of the PHP program reads the include file only when it reads it. In this way, the process of program execution can be simplified.

The purpose of the two of them is exactly the same, and it does not necessarily have to be placed in the front and in the middle. The most fundamental difference between them is the way errors are handled. 

If there is an error in require a file, the program will interrupt execution and display a fatal error 
. If there is an error in include a file, the program will not be in the middle, but will continue to execute and display a warning error. 

The following are supplements:

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

2. include() includes and runs the specified file. When the processing fails, include() will generate a warning, and the imported program code will be executed, and these programs will have the same function as the source file that calls the include() statement when executed. variable scope with the same position. You can import static pages from the same server. 

3. The function of include_once() is almost the same as include(), the 
only difference is that include_once() will first check whether the file to be imported has been imported elsewhere in the program, and if so, it will not. It will be imported again (this function is sometimes very important, for example, some functions that you define yourself are declared in the import, so if you import this file repeatedly in the same program, it will be easy to import when you import it for the second time. An error message occurs 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-in contents. When the processing fails, require() will cause a fatal error. 
This read and replace action happens when the PHP engine compiles your code, not when the PHP engine starts executing the compiled code (the way the PHP 3.0 engine works is to compile line by line, However, it has changed since PHP 4.0. In PHP 4.0, the entire program code is compiled first, and then the compiled program code is executed at one time, and 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, and if so, it will not import the same content again. 

5. require is 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 top of the PHP program. Before the PHP program is executed, it will first read the file specified by require and make it 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 web page reads the include file only when it reads it. This method simplifies the process of program execution.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324746934&siteId=291194637