First, the role
The include () and require () statements function to include and run the specified file.
Two, include () and require () difference
The two structures are exactly the same except how to handle failures. Include () generates a warning that the script will continue to run. Require () causes a fatal error and the script stops running.
In other words, if you want to stop processing the page when you encounter a missing file or encounter an error, use require (). Use include () if you want to continue processing the page if you encounter an error.
Note: When using include (), a syntax error in the containing file does not cause the program to stop until PHP 4.3.5, but after this version. That is, the script does not continue to execute when the include () file does not exist after PHP 4.3.5.
Third, include_once (), require_once ()
The include_once () statement contains and runs the specified file during the execution of the script. This behavior is similar to the include () statement, except that include_once () first determines whether the file has been included before and, if it is already contained, ignores the inclusion. Include_once () should be used in the case of nested inclusions, want to make sure it is only included once, to avoid problems such as function redefinition, variable assignment, and so on.
(Require_once () the same. )
Iv. issues to be noted
1, the path problem
Especially when nesting is included, be sure to note the path of the containing file.
For example, a file contains b files, b file contains c files, a,b,c files are not in the same folder, this time is often very easy to make mistakes.
Solution: Use an absolute path, that is, you can use the DirName (__file__) statement, which means to get the absolute path of the current script. such as: require_once (DirName (__file__). /config.php ');
2. Efficiency issues
Include_once (), require_once (), compared with include (), require (), are less efficient because they must at least determine if the file is included. The problem has been greatly improved in the PHP5 version, but there is still a difference in efficiency.