In PHP, include () and require () have the same functions. include (include_once) and require (require_once) both read the included file code to the specified location, however, there is a difference in their usage: (include () is a conditional include function, while require () is an unconditional include function)
1. Use different methods
(1) how to use require, such as require ("requireFile. php ");. This function is usually placed 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. This method can also be used to introduce a commonly used function to a webpage. The introduction is unconditional. It occurs before the program is executed, whether or not the conditions are true or not. (It may not be executed ).
(2) include usage methods such as include ("includeFile. php ");. This function 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. The introduction is conditional. When the program is executed, the import is performed only when the condition is set (the code generated by compilation can be simplified ).
For example, in the following example, if the variable $ somgthing is true, it will contain the file somefile:
Copy codeThe Code is as follows:
If ($ something ){
Include ("somefile ");
}
However, no matter what value $ something takes, the following code will include the file somefile into the file:
Copy codeThe Code is as follows:
If ($ something ){
Require ("somefile ");
}
The following interesting example fully demonstrates the differences between the two functions.
Copy codeThe Code is as follows:
$ I = 1;
While ($ I <3 ){
Require ("somefile. $ I ");
$ I ++;
}
In this Code, each loop contains the same file. Obviously, this is not the original intention of the programmer. From the code, we can see that this code will include different files in each loop. To complete this function, you must turn to the include () function ():
Copy codeThe Code is as follows:
$ I = 1;
While ($ I <3 ){
Include ("somefile. $ I ");
$ I ++;
}
2. errors are reported in different ways during execution.
Difference between include and require: if an error occurs when the include file is introduced, a prompt will be provided and the following code will be run. If an error occurs when the require file is introduced, A prompt is displayed, and the following code is stopped. For example:
Write two PHP files named test1.php and test2.php. Note that there is no file named test3.php in the same directory.
Test1.php
Copy codeThe Code is as follows:
<? PHP
Include ("test3.php ");
Echo "abc ";
?>
Test2.php
Copy codeThe Code is as follows:
<? PHP
Require ("test3.php ")
Echo "abc ";
?>
Browse the first file. Because the test999.php file is not found, we can see the error message. At the same time, abc is displayed under the error message, which may be similar to the following:
Warning: include (test3.php) [function. include]: failed to open stream: No such file or directory in D: \ WebSite \ test. php on line 2
Warning: include () [function. include]: Failed opening 'test3. php 'for declaration (include_path = '.; c: \ php5 \ pear ') in D: \ WebSite \ test. php on line 2
Abc (the following code is executed)
Browsing the second file, because the test3.php file is not found, we can see the error message, but the error message below does not show abc, you may see a situation similar to the following:
Warning: require (test3.php) [function. require]: failed to open stream: No such file or directory in D: \ WebSite \ test2.php on line 2
Fatal error: require () [function. require]: Failed opening required' test3. php '(include_path = '.; c: \ php5 \ pear ') in D: \ WebSite \ test. php on line 2
The following is not executed and ended directly.
In short, the calling during include is a process behavior with conditions, while require is a preset behavior with no conditions.