The first thing to say is that the core functions of the include, include_once, require, require_once are the same, which is equivalent to the code of the target Web page to be copied directly.
Basically, using include, include_once, require, require_once can achieve basic results without performance considerations. Basically love how to use it.
The minor differences are as follows:
For example, there is a simple print statement in 1.php:
The following program runs the result:
is 2 1 instead of 4 1 because both the include and require are introduced into the specified file, _once means that it is introduced only once, that is no longer introduced.
If written as:
The result will be 4 of 1.
It is important to note that although _once indicates that it is no longer introduced, this statement does not check whether the code in the introduced Web page is the same, such as a page with two identical code untitled.html and untitled1.html:
Untitled Document
If you have the following statement:
The result is this:
See the same section appear two times. _once simply uses the file name to determine if it has been introduced before.
Therefore, _once does not prevent the phenomenon of different names being introduced multiple times with the same resource.
The biggest difference between include and require is that, in addition to the way in which files are introduced, the include generates a warning when a file is introduced and the script continues execution, require causes a fatal error and the script stops executing.
For example, if a.php does not exist, the following code will still output B:
and
It will not.
There are also the following differences:
Include () is a conditional include function, while require () is an unconditional include function
if (false) { include ' file.php ';//file.php is not introduced} if (false) { require ' file.php ';//file.php will be introduced}
Include has a return value, and require does not
$retVal = include (' somefile.php '), if (!empty ($retVal)) { echo "file contains success";} else{ echo "file contains failed";}
Yes, I can.
Include () The file that needs to be referenced every time to read and evaluate, require () execution of the file needs to be referenced only once, the actual execution of the file needs to be referenced to replace the require () statement, You can see that if you have code that contains one of these directives and code that might execute multiple times, use require () is more efficient, use include () if you read different files each time you execute the code, or if you have loops that pass through a set of file iterations.
Require usually use the method, this function is usually placed in the front of the PHP program, the PHP program before execution, will be read into the require specified by the introduction of the file, so that it becomes a part of the PHP program page. Commonly used functions, you can also use this method to introduce it into the Web page. Include typically uses methods, which are generally placed in the processing part of the process control. The PHP Program page reads the include file before it is read in. This way, you can simplify the process when the program executes.
In addition to the question of whether or not parentheses are appended to the include and require, theoretically: the Include and require parentheses do not differ from the execution result, but the parentheses are less efficient, so there is no parentheses behind the parentheses.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.