How to load files in php: 1. Use the "include 'file name'" statement to load; 2. Use the "require 'file name'" statement to load; 3. Use the "include_once 'file name'" statement to load ;4. Use the "require_once 'file name'" statement to load.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
There are four statements for loading files in PHP: include, require, include_once, require_once.
require: The require function is generally placed at the front of the PHP script. Before PHP is executed, it will first read in the file specified by require, include and try Execute the imported script file. The way require works is to improve the execution efficiency of PHP. After it is interpreted once in the same web page, it will not be interpreted the second time. But similarly, because it will not repeatedly interpret the imported file, you need to use include when using loops or conditional statements to introduce files in PHP.
include: can be placed anywhere in the PHP script, usually in the processing part of the process control. When the PHP script is executed to the file specified by include, it will be included and attempted to execute. This method can simplify the process of program execution. When encountering the same file for the second time, PHP will still re-interpret it again. The execution efficiency of include is much lower than that of require. At the same time, when the user-defined function is included in the imported file, PHP will have the problem of repeated function definition during the interpretation process. .
require_once/include_once: The functions are the same as require/include respectively. The difference is that when they are executed, they will first check whether the target content has been imported before. If it has been imported, Then the same content will not be reintroduced again.
include has a return value, while require has no return value
include is loading the file On failure, a warning (E_WARNING) is generated and the script continues execution after the error occurs. So include is used when you want to continue execution and output results to the user.
//test1.php <?php include './tsest.php'; echo 'this is test1'; ?> //test2.php <?php echo 'this is test2\n'; function test() { echo 'this is test\n'; } ?> //结果: this is test1
require will generate a fatal error (E_COMPILE_ERROR) when loading fails, and the script will stop executing after the error occurs. Generally used when subsequent code depends on the loaded file.
//test1.php <?php require './tsest.php'; echo 'this is test1'; ?> //test2.php <?php echo 'this is test2\n'; function test() { echo 'this is test\n'; } ?>
Result:
//test1.php <?php include './test2.php'; echo 'this is test1'; include './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1this is test2 //test1.php <?php include './test2.php'; echo 'this is test1'; include_once './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1 //test1.php <?php include_once './test2.php'; echo 'this is test1'; include './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1this is test2 //test1.php <?php include_once './test2.php'; echo 'this is test1'; include_once './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //结果: this is test2this is test1
require and require_once: The same difference as include and include_once.
//test1.php <html> <body> 主文件开始位置: <?php echo "<br> 主文件中位置 A"; include "./test2.php"; //要载入的文件 echo "<br> 主文件中位置 B"; ?> <br> 主文件结束位置 </body> </html> //test2.php <br> 被载入文件位置 1 <?php echo "<br> 被载入文件位置 2"; ?> <br> 被载入文件位置 3
Result analysis:
Recommended study: "
PHP Video Tutorial
"The above is the detailed content of What are the ways to load files in php. For more information, please follow other related articles on the PHP Chinese website!