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

The difference between include and require in php

巴扎黑
巴扎黑Original
2016-11-24 11:48:51974browse

require and include provide different usage flexibility. I u Require uses how to use ("myrequirefile.php"); This function 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.
                                                                                                                                                 to be used like include("MyIncludeFile.php"); . This function is usually placed in the processing section of flow control. The PHP program web page reads the include file only when it reads it. In this way, the process of program execution can be simplified.


1.include is loaded when used

2.require is loaded at the beginning

3._once suffix indicates that the loaded one is not loaded

The php system has a pseudo-compilation process when loading the php program , can make the program run faster. But the include document is still for explanation and execution


An error occurred in the include file, and the main program continued to execute
An error occurred in the require file, and the main program also stopped
So if an error in the included file has little impact on the system (such as the interface file), use include, otherwise use require


The following documents also apply to require(). The two structures are identical except for how they handle failure. 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. Also make sure to set the appropriate include_path.

The require() function replaces itself with the contents of the given file. This replacement process occurs when the PHP engine compiles the code, not during execution. It does not calculate first like include(). The require() function is more used in static elements, while include() is used more in dynamic elements. Similar to include_once(), require_once() will first check whether the given code has been inserted. If the code already exists, it will not be inserted again.


Statement:
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