There are two ways to refer to a file:require and include. Two ways to provide different use elasticity.
1 require("MyRequireFile.php"); . How to userequire .
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.
2, The use of the include include("MyIncludeFile.php"); methods such as.
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 did not.
2. Include () includes and runs the specified file: include () generates a warning when processing fails,The imported program code will be executed, and these programs will be executed 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.include_once ()The role andinclude ()is almost the same
The only difference isinclude_once ()Will first check that the file to be imported is already in the other places in the program has been imported, if you do not repeat the import (this function is sometimes very important, for example, to import the inside announced some of your own definition of functions, then if the same program repeatedly import this file, The error message occurs at the time of the second import, because PHP does not allow functions of the same name 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. Asinclude_once (), require_once ()The contents of the target file will be checked first before it has been imported, and if so, the same content will not be re-imported again.
6. 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 inProcess ControlIn the processing section of the PHP program, the Web page reads the include file before it is read in. This way you can simplify the process of executing the program.
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4559064.html
[PHP Learning Tutorial]001. Reference files (require & include)