Detailed Explanation of the Differences Between include and require in PHP
Category Programming Technology
1. Overview
The require() statement performs similarly to include(), both including and running the specified file. The key difference lies in how they handle the files: for include(), the file is read and evaluated each time it is executed; for require(), the file is processed only once (essentially, the file content replaces the require() statement). This means that for code that may be executed multiple times, using require() is more efficient. On the other hand, if the code reads different files each time it is executed, or if there is a loop iterating through a set of files, include() should be used.
The usage of require() is as follows:
require("myfile.php")
This statement is typically placed at the beginning of the PHP script. Before executing the PHP script, the file included by the require() statement is read into it, making it part of the script.
include() is used in the same way as require:
include("myfile.php")
This statement is usually placed within the flow control section of the script.
When the PHP script reaches the include() statement, it reads in the included file. This method simplifies the flow of the program during execution.
- include loads when used
- require loads at the beginning
- _once suffix indicates that already loaded files will not be loaded again
PHP has a pseudo-compilation process during loading, which speeds up program execution. However, the included files for include() are still interpreted. If there is an error in the included file, the main program continues to execute with include(), but stops with require(). Therefore, if the impact of an included file error is minor (e.g., interface files), use include(); otherwise, use require().
The require() and include() statements are language constructs, not true functions. They can be used like other PHP language constructs, such as echo(), which can output strings in both echo("ab") and echo "abc" forms. The require() and include() statements can also be used without parentheses directly with parameters.
include_once() and require_once() statements also include and run the specified file during script execution. This behavior is similar to include() and require(), and they are used in the same way. The only difference is that if the code from the file has already been included, it will not be included again. These statements should be used to ensure that a file is included only once during script execution, to avoid issues like function redefinition and variable reassignments.
2. Detailed Explanation
2.1 Error Handling
include includes files and, if an error occurs, provides a warning and continues executing the rest of the code.
require includes files and, if an error occurs, provides a warning and stops executing the rest of the code.
To illustrate this, create two PHP files named test-include.php and test-require.php. Ensure that there is no file named test-nothing.php in the same directory.
test-include.php
<?php
include 'test-nothing.php';
echo 'abc';
?>
test-require.php
<?php
require 'test-nothing.php';
echo 'abc';
?>
Browsing http://localhost/test-include.php, since the file test-nothing.php is not found, you will see an error message, and below that, 'abc' is displayed. You might see something like:
Warning: include(test-nothing.php) [function.include]: failed to open stream: No such file or directory in D:\www\test-include.php on line 2
Warning: include() [function.include]: Failed opening 'test-nothing.php' for inclusion (include_path='.;C:\php5\pear') in D:\www\test-include.php on line 2
abc
Browsing http://localhost/test-require.php, since the file test-nothing.php is not found, you will see an error message, but 'abc' will not be displayed. You might see something like:
Warning: require(test-nothing.php) [function.require]: failed to open stream: No such file or directory in D:\www\test-require.php on line 2
Fatal error: require() [function.require]: Failed opening required 'test-nothing' (include_path='.;C:\php5\pear') in D:\www\test-require.php on line 2
2.2 File Inclusion Methods
include() requires the referenced file to be read and evaluated each time it is executed, while require() processes the referenced file only once (essentially, the file content replaces the require() statement). If there is code that includes these directives and may be executed multiple times, using require() is more efficient. If the code reads different files each time it is executed or iterates through a set of files, use include(). You can set a variable for the filename to include when using include().
>
Original link: https://blog.csdn.net/shenpengchao/article/details/52326233
** Share My Notes
-
-
-