: This article mainly introduces file references (include, require, include_once, require_once) in php. For more information about PHP tutorials, see. File referenced by the include statement
Note: When using include to apply external files, the external files will be introduced and read only when the code executes the include statement. When an error occurs in the referenced external file, the system only provides one warning, while the entire php file continues to run down.
Include ("top. php ");
Include ("main. php ");
Include ("bottom. php ");
?>
Use the require statement to reference a file
Before the php file is executed, the php parser replaces the require statement with all the content of the referenced file, and then forms a new php file with other statements other than the require statement, finally, execute the program code according to the new php file.
Note: Because the require statement is equivalent to completely copying the content of another source file to a file, it is generally placed at the starting position of the source file to reference the public function files and public class files to be used.
Differences between include and require statements
When you use the require statement to call a file, if the called file is not found, The require statement will output an error message and terminate script processing immediately. When the include statement does not find the file, it will output a warning and will not terminate the script processing.
When you use the require statement to call a file, as long as the program is executed, the external file will be called immediately. when you use the include statement to call an external file, only when the program executes this statement, to call an external file.
Use the include_once statement to reference a file
The include_once statement is used to check whether the file has been applied in other parts of the page before the file is imported. If yes, the file is not referenced repeatedly and the program can only reference the file once.
For example, if some user-defined functions exist in the file to be imported, if the file is repeatedly imported in the same program, an error will occur during the second import, because PHP does not allow repeated declarations of functions with the same name
Use the require_once statement to reference a file
The extension of the require statement in the require_once statement. its function is similar to that in the require statement. The difference is that when the require_once statement is applied, check whether the file to be referenced has been referenced elsewhere in the program. If yes, the file will not be called repeatedly.
For example, if two identical files are referenced in the same page by applying the require_once statement at the same time, only the first file is executed during output, and the second referenced file is not executed.
Use of include_once and require_once statements
When an error occurs when the include_once statement calls an external file during script execution, a warning is generated, and the require_once statement causes a fatal error.
The purpose is to ensure that a contained file can only be contained once. using these two statements can prevent accidental multiple times containing the same function library, leading to repeated function definitions and errors.
The above describes the file references (include, require, include_once, require_once) in php, including some content, and hope to help friends who are interested in PHP tutorials.