The usage difference between include and require in PHP
in PHP into, include () and require () the same function , include (include_once) and require (require_once) is to put the included file code into the specified location, But there is a difference in the use of the two: (include () is conditional include function, and require () is unconditionally included function )
1, different ways of use
(1) Require use method such as require ("requirefile.php");. This function is usually placed at the front of the PHP program, and before the PHP program is executed, it is read into the file specified by require to make it a part of the PHP program's Web page. Commonly used functions, you can also use this method to introduce it into the Web page. The introduction is unconditional and occurs before the execution of the program, regardless of whether the condition is established or not, to be imported (may not be executed).
(2) Include use method such as include ("includefile.php");. This function is generally placed in the processing section of the Process Control. The PHP Program page reads the include file when it is read. This way, you can simplify the process of executing the program. Ingestion is conditional, and occurs when the program executes, only when the condition is set (which simplifies compiling the generated code).
For example, in the following example, if the variable $somgthing is true, it will contain the file Somefile:
if ($something) {
include ("Somefile");
}
However, regardless of the $something value, the following code will include the file somefile into the file:
if ($something) {
require ("Somefile");
}
The following interesting example illustrates the difference between the two functions.
$i = 1;
While ($i < 3) {
require ("Somefile. $i");
$i + +;
}
in this code, every time a loop is made, the program will include the same file. Obviously this is not the intention of the programmer, and from the code we can see that this code wants to include different files in each loop. If you want to complete this function, you must ask for the function include ():
$i = 1;
While ($i < 3) {
include ("Somefile. $i");
$i + +;
}
2. The execution times are different in the wrong way
The difference between include and require: include introduces the file, if encountered error, will give a hint, and continue to run the following code, require the introduction of the file, if encountered errors, will give a hint, and stop running the code below. For example, the following example:
Write two php files, named test1.php and test2.php, note the same directory, do not exist a name is test3.php file.
test1.php
Include ("test3.php");
echo "ABC";
?>
test2.php
Require ("test3.php")
echo "ABC";
?>
Browse the first file, because we did not find the test999.php file, we see the error message, at the same time, the error message below the display of ABC, you can see a similar situation below:
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 inclusion (include_path= '.; C:\php5\pear ') in D:\WebSite\test.php on line 2
ABC ( The following is executed )
Browse the second file, because we did not find the test3.php file, we see the error message, but, the error message is not shown below the ABC, you can see the situation is similar to the following:
Warning:require (test3.php) [function.require]: failed to open stream:no such file or directory in D:\WebSite\test2.php o N 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, the direct end
In summary, include when executing when invoked, is a process behavior, conditional, and require is a preset behavior, unconditional.