Home > Article > Backend Development > What are the differences between several include file functions in PHP?
Difference: 1. If include encounters an error, it will give a prompt and continue execution; while require will terminate program execution. 2. If the included file in require_once and include_once has already been included, it will not be included again, but include and require will.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
File inclusion refers to the inclusion of another source file All contents are included in the current source file for use, which is also commonly referred to as introducing external files. Referencing external files can reduce code reusability and is an important skill in PHP programming.
PHP provides 4 very simple but useful include statements, namely include statement, require statement, include_once statement and require_once statement. There are certain differences in the use of these four statements. Each is explained in detail below.
1. include statement
When using the include statement to include an external file, the external file will only be included when the code is executed to the include statement. , when an error occurs in the included external file, the system will give a warning, and the entire PHP program will continue to execute.
The syntax format of the include statement is as follows:
include('filename') 或者 include 'filename'
where filename is the file path to be included (both relative and absolute paths are acceptable), filename is a string, so single quotes are required' ' or wrapped in double quotes " ". At the same time, the brackets after include can also be omitted. When the brackets are omitted, include needs to be separated from the following filename by a space.
For the convenience of demonstration, here we prepare a demo.php file and simply define a $str variable in it, as shown below:
<?php $str = 'https://www.php.cn/'; ?>
[Example] Use the include statement to include the demo .php file, the code is as follows:
<?php include './demo.php'; echo $str; ?>
The running result is as follows:
https://www.php.cn/
2. require statement
require The usage of the statement is similar to the include statement, both of which implement references to external files. Before the PHP file is executed, the PHP parser will replace the require statement with the entire content of the referenced file, then form a new PHP file with other statements except the require statement, and finally execute the program code according to the new PHP file.
Note: Because the require statement is equivalent to completely copying the contents of another source file into this file, it is generally placed at the beginning of the source file to reference the public function files and files that need to be used. Public class files, etc.
The require statement is almost exactly the same as the include statement. The difference is that when the included file does not exist or there is an error, the require statement will issue a Fatal error and terminate program execution, while the include statement will issue a Warining warning. But the program will continue to execute downwards.
The syntax format of the require statement is:
require(filename) 或者 require 'filename'
The parameter filename is the path of the file to be included, and its characteristics are the same as the parameters in the include statement.
[Example] Use the require statement to include the demo.php file defined above. The code is as follows:
<?php require './demo.php'; echo $str; ?>
The running results are as follows:
https://www.php.cn/
3. include_once statement
The include_once statement is similar to the include statement. The only difference is that if the included file has already been included, it will not be included again. include_once can ensure that the same file is only included once during script execution to avoid problems such as function redefinition and variable reassignment.
Let’s adjust the demo.php file defined above as follows:
<?php echo 'PHP中文网<br>'; echo 'https://www.php.cn/'; ?>
[Example] Use the include_once statement to include the demo.php file, the code is as follows:
<?php include_once './demo.php'; include_once './demo.php'; include_once './demo.php'; ?>
The running results are as follows:
PHP中文网 https://www.php.cn/
4. require_once statement
require_once statement is an extension of require statement, and its function is the same as require The statements are basically similar. The difference is that when applying the require_once statement, it will first check whether the file to be included has been included elsewhere in the program. If so, the file will not be included again.
[Example] Use the require_once statement to include the demo.php file. The code is as follows:
<?php require_once './demo.php'; require_once './demo.php'; require_once './demo.php'; ?>
The running results are as follows:
PHP中文网 https://www.php.cn/
Recommended learning: "PHP Video tutorial》
The above is the detailed content of What are the differences between several include file functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!