Home >Backend Development >PHP Tutorial >File references in php (include, require, include_once, require_once)
Use include statements to reference files
Note: When using include to apply external files, the external files will be introduced and read only when the code is executed to the include statement. When an error occurs in the referenced external file, the system will only A warning is given, and execution of the entire php file continues downward.
include("top.php");
include("main.php");
include("bottom.php"); The require statement references the file
Note: Because the require statement is equivalent to completely copying the contents of another source file into the file, it is generally placed at the beginning of the source file to reference the public function files and public class files that need to be used.
The difference between include statement and require statement When using the require statement to call a file, if the called file is not found, the require statement will output an error message and terminate the script processing immediately. The include statement will output a warning when the file is not found and will not terminate the script processing.When using the require statement to call a file, the external file will be called immediately as soon as the program is executed; when calling the external file through the include statement, the external file will only be called when the program executes the statement.
Use the include_once statement to reference the file Using the include_once statement will check whether the file has been used in other parts of the page before importing the file. If so, the file will not be referenced repeatedly. The program can only Quote once.For example: There are some custom functions in the file to be imported. If you import this file repeatedly in the same program, an error will occur during the second import because PHP does not allow functions with the same name to be declared repeatedly
Use the require_once statement to reference files The require_once statement is an extension of the require statement. Its function is basically similar to the require statement. The difference is that when applying the require_once statement, it will first check whether the file to be referenced is already in the require_once statement. If it is referenced elsewhere in the program, the file will not be called again.For example: if the require_once statement is applied at the same time to reference two identical files in the same page, then only the first file will be executed during output, and the second referenced file will not be executed.
The difference between the use of include_once and require_once statements include_once statement generates a warning when an error occurs when calling an external file during script execution, while the require_once statement causes a fatal error.The purpose is to ensure that an included file can only be included once. Using these two statements can prevent the same function library from being accidentally included multiple times, resulting in repeated definitions of functions and generating errors. The above introduces the file references in PHP (include, require, include_once, require_once), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.