Explanation of the difference between require and require_once in php_PHP tutorial

WBOY
Release: 2016-07-13 10:36:47
Original
864 people have browsed it

include() and require(): statements include and run the specified file.
include() produces a warning and require() results in a fatal error. In other words, use require() if you want to stop processing the page if a missing file is encountered. This is not the case with include() and the script will continue to run.

The

require_once() statement includes and runs the specified file during script execution. This behavior is similar to the require() statement, the only difference is that if the code in the file is already included, it will not be included again. The
include_once() statement includes and runs the specified file during script execution. This behavior is similar to the include() statement, the only difference is that if the code in the file is already included, it will not be included again. As the name of this statement implies, it will only be included once.

1.include() function will read the specified file and execute the program inside.

For example: include('/home/me/myfile');

The program code in the imported file will be executed, and when executed, these programs will have the same variable scope as the location where the include() function is called in the source file. You can import static files from the same server, or even import files from other servers by combining the include() and fopen() functions.

2. The function of include_once() is almost the same as include()

The only difference is that the include_once() function will first check whether the file to be imported has been imported elsewhere in the program. If so, it will not import the file again (this function has Timing is very important. For example, if the file to be imported declares some functions that you have defined yourself, then if you import the file repeatedly in the same program, an error message will occur during the second import, because PHP does not Allows a function with the same name to be declared a second time).

3.require() function will read the contents of the target file and replace itself with the read contents.

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 a line Execute one line, but this has changed with 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.

4. Like the include_once() function, the require_once() function will first check whether the content of the target file has been imported before. If so, the same content will not be imported again.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736849.htmlTechArticleinclude() and require(): statements include and run the specified file. include() produces a warning and require() results in a fatal error. In other words, if you want to encounter lost files...
Related labels:
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