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() will cause a fatal error (an error occurred , the script stops executing)
require() : If the file does not exist, a fatal error will be reported. The script will stop executing
include() : If the file does not exist, a warning will be given, but the script will continue to execute
Here Special attention should be paid to: when the include() file does not exist, the script continues to execute. This situation only occurs before PHP 4.3.5. It is recommended to use
require_once() and include_once() to detect whether the file is duplicated. Include. 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 The content replaces the require() statement). This means that if you have 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 you can set a variable for the file name you want to include. This variable is used when the parameter is include().
3. The two methods provide different flexibility of use.
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 in 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 process 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.
require will be included even when the condition bit is FALSE, and 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() function is almost the same as require() 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_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. For example, a file defines a If the file is loaded twice, an error will occur. The above is the difference between the PHP include() and
require
() methods.
The above introduces the difference between the PHP include and require methods, including the require content. I hope it will be helpful to friends who are interested in PHP tutorials.