Home > Backend Development > PHP Tutorial > The difference between include() and require() in php

The difference between include() and require() in php

WBOY
Release: 2016-07-25 09:05:41
Original
1030 people have browsed it
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, and the imported program code will be executed.

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 the include() statement called in the source file when executed. position in the same variable scope. 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 be imported again (this function is sometimes very important, For example, if the file you want to import declares some functions that you have defined yourself, then if you import this file repeatedly in the same program, an error message will occur during the second import, because PHP does not allow functions with the same name to be repeated. declared a second time). 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 and execute one line at a time. But things have 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, the same content will not be imported again. 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 web page reads the included file only when it reads it. This method can simplify the process of program execution.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template