The difference between include of and require of in PHP

  • 2020-03-31 20:30:15
  • OfStack

The method of using require is require(" myrequirefile.php "). < / code >. This function is usually placed at the beginning of a PHP program, which, before executing, reads in the file specified by require, making it part of the PHP program's web page. Common functions, can also be introduced into the web page this method.

Include USES the method include(" myincludefile.php "); < / code >. This function is typically placed in the processing part of the process control. The PHP program page reads the include file when it reads it. In this way, you can simplify the process of program execution.

The two are used exactly the same, not necessarily one in front and one in the middle. The fundamental difference between them is the way they handle errors.

If there is an error in requiring a file, the program interrupts execution and displays a fatal error
Include if there is an error in a file, the program will not be in the middle, but will continue with a warning error.

The following is the supplement:

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

Include () includes and runs the specified file. Include () generates a warning when processing fails, and the program code that is imported will be executed, and the program will have the same range of variables that are called to the include() statement in the source file. You can import static pages from the same server.

3. Include_once () does almost the same thing as include()
The only difference is that include_once () will first check to import file is already in other areas of the program to be imported, if any, will not repeat the import (this feature is very important, sometimes, for example, the inside of you want to import some your own defined function, so if in the same program to repeat this file to import, the second import will occur when the error message, because PHP does not allow the same name declared function is repeated for the second time).

4. Require () reads in the contents of the target file, and substituting itself for those contents causes a fatal error if the processing fails.
This read and substitution of the movement is in the PHP engine when compile your code, rather than in the PHP engine start executing the compiled code (PHP 3.0 engine works is to compile a line to perform a line, but in PHP 4.0 after change, PHP 4.0 is to compile the whole program code is complete, then insert the compiled code is performed at a time, in the process of compiling will not perform any program code). Require () typically imports static content, while include() is good for importing dynamic program code.

5. Like include_once(), require_once() checks to see if the contents of the target file have been imported before, and if so, does not import the same contents again.

5. Require is unconditional. That is, if you add require to a process, it does require first, regardless of whether the condition is true or not.

7. Require is usually placed at the beginning of a PHP program. Before a PHP program can execute, it reads in the file required to be imported, making it part of the PHP program's web page. Common functions, can also be introduced into the web page this method.

Include is typically placed in the processing section of process control and the PHP program page reads the include file when it reads it. This approach simplifies the flow of execution.

(link: #)