1. Summary
The performance of the Require () statement is similar to the include (), which includes and runs the specified file. The difference is that for the include () statement, the file is read and evaluated every time it is executed, whereas for require () the file is processed only once (in fact, the file content replaces the require () statement). This means that it is more efficient to use require () if you can execute code more than once. On the other hand, use the Include () statement if you are reading a different file each time you execute the code, or if you have a loop that iterates through a set of files.
Require uses such as: Require ("myfile.php"), this statement is usually placed at the top of the PHP script program. Before executing, the PHP program reads the file introduced into the Require () statement and makes it a part of the PHP script file. Include using the same method as require: include ("myfile.php"), and this statement is typically placed in the processing section of the Process Control. The php script file reads the included file when it reads the include () statement. This way, you can simplify the process of executing your program. Incluce load require at the start of loading _once suffix indicates loaded not loaded
PHP System when loading PHP program has a pseudo compile process, can make the program run faster. However, the Incluce document is still interpreted for execution. Include file error, the main program continues to execute, require file error, the main program also stopped, so the file contains error on the system has little impact, such as interface file with include, otherwise use require.
The Require () and include () statements are language constructs, not real functions, like other language structures in PHP, such as Echo (), which can be in the form of Echo ("AB"), or output string ABC in the form of Echo "ABC". Require () and include () statements can also be directly added without parentheses.
The include_once () and require_once () statements also include running the specified file during script execution. This behavior is similar to the include () statement and require (), as does the use method. The only difference is that if the code in the file is already included, it will not be included again. These two statements should be used to ensure that the same file may be included more than once during the execution of the script, to avoid problems such as function redefinition and variable redistribution. 2. Details 2.1 Error
Include when the file is introduced, if you encounter an error, you will be prompted and continue to run the code below.
When require introduces a file, if you encounter an error, it prompts you and stops running the code below.
Use examples to speak, write two php files, name test-include.php and test-require.php, note the same directory, do not exist a name is test-nothing.php file.
test-include.php
<?php
Include ' test-nothing.php ';
Echo ' abc ';
?>
test-require.php
<?php
Require ' test-nothing.php ';
Echo ' abc ';
?>
Browse http://localhost/test-include.php, because did not find the test-nothing.php file, we saw the error message, at the same time, the error message below shows the ABC, you can see is similar to the following situation:
Warning:include (test-nothing.php) [function.include]: failed to open stream:no such file or directory in D:\www\test-inc Lude.php on line 2
Warning:include () [function.include]: Failed opening ' test-nothing.php ' for inclusion (include_path= '.; C:\php5\pear ') in D:\www\test-include.php on line 2
Abc
Browse http://localhost/test-require.php, because did not find the test-nothing.php file, we saw the error message, but, the error message does not show the bottom of ABC, you can see is similar to the following situation:
Warning:require (test-nothing.php) [function.require]: failed to open stream:no such file or directory in D:\www\test-req Uire.php on line 2
Fatal Error:require () [function.require]: Failed opening required ' test-nothing ' (include_path= '.; C:\php5\pear ') in D:\www\test-require.php on line 2 2.2 piece reference
Include () and require () have the same functionality, but there are some differences in usage, include () is a conditional include function, and require () is an unconditional include function.
For example, if the variable $somg is true, the file somefile.php will be included:
if ($some) {
Include ' somefile.php ';
}
But no matter what value the $some takes, the following code will include the file somefile.php in the file:
if ($something) {
Require ' somefile.php ';
}
The following examples fully illustrate the differences between the two functions:
$i = 1;
while ($i < 3) {
Require "somefile. $i. php";
$i + +;
}
As you can see from the above code, each time the loop, the program will be included in the same file, obviously this is not what we want, you can see that the code wants to be in each loop, the different files included, if you want to complete this function, only use the function include ():
$i = 1;
while ($i < 3) {
Include "Somefile. $i. php";
$i + +;
} 2.3 File reference mode
The file to be referenced when
include () executes is read and evaluated each time, and require () the file to be referenced is processed only once (the content of the file that needs to be referenced is replaced require () statement, you can see that if you have code that contains one of these directives and code that may execute multiple times, using require () is more efficient, if you are reading different files each time you execute the code or if you have a loop over a set of file iterations, use include () to set the variable for the file name you want to include. Use this variable when the parameter is include ().