Home > Backend Development > PHP Tutorial > The difference between PHP include() and require() methods_PHP tutorial

The difference between PHP include() and require() methods_PHP tutorial

WBOY
Release: 2016-07-15 13:26:54
Original
841 people have browsed it

PHP’s include() and require() are two methods of including external files. Many beginners may not understand the difference between these two methods. The following summarizes the differences between PHP include() and require():

1: Loading failures are handled differently:

include() will generate a warning, while require() results in a fatal error (an error occurs and the script stops executing)

require(): If the file does not exist, a fatal error will be reported. The script stops executing

include( ): If the file does not exist, a warning will be given, but the script will continue to execute

What is particularly important to note here is: when the include() file does not exist, the script will continue to execute. This situation only occurs in Before PHP 4.3.5

it is recommended to use require_once() and include_once() to detect whether files are included repeatedly.

2.php performance

For include(), the file must be read and evaluated every time when include() is executed;

For require(), the file is only processed once (in fact, the file content replaces the require() statement).

This means that if there is code that contains one of these instructions and code that may be executed multiple times, it is more efficient to use require().

On the other hand, if you read a different file each time the code is executed, or have a loop that iterates through a set of files, use include(),

because it can give you ideas Sets a variable with the name of the file to include. This variable is used when the argument is include().

3. The two methods provide different usage flexibility.

require is used like require("./inc.php"); . It is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read the file specified by require and make it a part of the PHP program web page.

include is used like include("./inc/.php"); . Generally placed in the processing section of flow control. The PHP program web page reads the include file when it reads it. In this way, the process of program execution can be simplified.

require will be included even when the condition bit is FALSE, while include will only be executed when it reaches the changed position. 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 function of require_once() is almost the same as require(). 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. The function of include_once() is almost the same as include().

The function of require_once is to check whether the file has been loaded before. If it has not been loaded, load it. If it has been loaded, it will not be loaded again, such as a file definition. If a type is loaded twice, an error will occur

The above is the difference between PHP include() and require() methods.


http://www.bkjia.com/PHPjc/446569.html

truehttp: //www.bkjia.com/PHPjc/446569.htmlTechArticlePHP’s include() and require() are two methods of including external files. For these two methods, there are Many beginners may not understand the difference. Let’s summarize PHP include() and re...
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