Require of the use of the method such as require("MyRequireFile.php"); . This function is usually placed at the top of the PHP program, PHP program before executing, will first read into the require designated to introduce the file, so that it becomes part of the PHP Program Web page. A commonly used function can be introduced into a Web page in this way.
IncludeUse methods such asinclude("MyIncludeFile.php");。 This function is typically placed in the processing section of the Process Control. The PHP Program page reads the included file when it is read. This way, you can simplify the process of executing a program.
The use of the two is exactly the same, not necessarily which one is in the front and which is in the middle. The fundamental difference between them is that the way they handle the error is different.
Require a file is faulty, the program interrupts execution and displays a fatal error
Include a file with errors, then the program will not midrange, but continue execution, and display a warning error.
The following are supplementary:
1. Include has a return value, and require does not.
2. Include () includes and runs the specified file when the process fails, include () generates a warning, the program code being imported is executed, and these programs have the same range of variables as the location of the call to the include () statement in the source file at the time of execution. You can import static pages from the same server.
3. The role of Include_once () and include () are almost identical
The only difference is that include_once () first checks whether the file being imported has been imported elsewhere in the program, if you do not have to repeat the import again (this feature is sometimes important, for example, to import the declaration of some of your own well-defined functions, If you import the file repeatedly in the same program, the error message will occur the second time you import it, because PHP does not allow functions of the same name to be repeated for the second time.
4. require () reads the contents of the target file and replaces itself with these read content require () in the case of a failure to cause a fatal error.
This read-and-replace action occurs when the PHP engine compiles your program code. Instead of when the PHP engine starts executing the compiled program code (the PHP 3.0 engine works by compiling one line of execution, but it changes after PHP 4.0), PHP 4.0 is to complete the entire program code after all compiled, and then compile the program code once completed, in the process of compiling will not execute any program code. Require () is typically used to import static content, and include () is suitable for importing dynamic program code.
5. As include_once (), require_once () first checks that the contents of the target file have been imported before, and if so, the same content will not be imported again.
5. Require is unconditional inclusion that is, if a process to join the require, regardless of whether the conditions are established will first execute require.
7. Require is usually placed at the top of the PHP program, the PHP program before executing, will first read into the require specified the introduction of the file, so that it becomes part of the PHP Program Web page. A commonly used function can be introduced into a Web page in this way.
8. Include is generally placed in the process control of the processing part of the PHP Program Web page read the include file, it is read in. This way you can simplify the process of executing a program.
Require (), include (), require_once () and include_once () difference