PHP Include () and require () are two methods that contain external files. Many beginners may not understand the differences between the two methods. The following summarizes the differences between PHP Include () and require:
1: loading failure is handled in different ways:
Include () generates a warning, while require () causes a fatal error (an error occurs and the script stops executing)
Require (): If the file does not exist, a fatal error is reported. The script is stopped.
Include (): If the file does not exist, a warning is provided, but the script continues to run.
Note: When the include () file does not exist, the script continues to run. This occurs only before PHP 4.3.5.
We recommend that you use require_once () and include_once () to check whether the file contains duplicates.
2. php Performance
For include (), the file should 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 one of these commandsCodeIt is more efficient to use require () than to execute code that may be executed multiple times.
On the other hand, if different files are read each time the code is executed, or there is a loop through a set of file stacks, 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. Two methods provide different use elasticity.
The use of require is as follows: require ("./Inc. php ");. Usually put at the beginning of the PHP program. Before the PHP program is executed, it will first read the file specified by require to make it a part of the PHP program webpage.
Include usage methods such as include ("./INC/. php ");. It is generally placed in the Process Section of process control. The include file is read on the PHP webpage. In this way, you can simplify the process during program execution.
Require will be included even when the condition bit is false, and include will be executed only when the change position is executed.
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 has been included, it will not be included again. The functions of the require_once () function are almost the same as those of 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 has been included, it will not be included again. The role of the include_once () function is almost the same as that of the include () function.
The function of require_once is to check whether the file has been loaded before. If the file has not been loaded, it will not be loaded again if it has been loaded, for example, a file defines a type. If the file is loaded twice, an error occurs.
The above is the difference between the PHP Include () and require () methods.