PHP has four functions that contain files: include (), include_once (), require (), and require_once (). Figuring out their differences is one of the basics of learning PHP and avoids the unnecessary hassle of writing code.
include ()
1. Mode of invocation: include ("/path/to/filename")
2. Note: the include () statement will contain the file specified by the parameter where it is invoked, with the effect of copying the contents of a file where include () appears. When using include (), parentheses can be ignored, such as include "/path/to/filename".
3. Traps: Through If...else ... Conditional statement to determine whether to include a file is a strange phenomenon. Such as
<?php
if (expression)
Include ("/path/to/filename");
Else
Include ("/path/to/anotherfilename");
?>
The above code may be wrong to run. Why is that? The Include () function simply copies the contents of the file to where the include () function appears, if the file contains multiple lines of PHP statements without using {} to compose the code quickly? The whole if...else ... The logic is messed up. So, this code should write this:
<?php
if (expression) {
Include ("/path/to/filename");
}
else{
Include ("/path/to/anotherfilename");
}
?>
This will ensure that the included files are in the code fast.
include_once ()
1. Mode of invocation: include_once ("filename")
2. Note: As the name implies, contains only one time the file. That is, if the file is already contained in the context, it is no longer included.
3. Traps: Have the same traps as include () functions.
require ()
1. Mode of invocation: Require ("filename")
2. Note: In addition to the following two points, the function is the same as include (): (1) regardless of where require () appears in the program fragment, it can include the file. Consider the following procedure:
<?php
if (false) {
Require ("/path/to/filename");
}
else{
Require ("/path/to/anotherfilename");
}
?>
The above statement includes both filename and anotherfilename two files, even if the first condition tests a condition that is false. (2) if the require () error occurs (for example, the require file does not have an error), the PHP script program will stop executing, but include () does not occur.
3. Trap: Have the same trap as include ().
require_once ()
1. Mode of invocation: require_once ("filename")
2. Note: Other functions are the same as require () except that they contain only one file at a time.
3. Trap: Have the same trap as require ().