What is the way PHP introduces files? PHP introduces a file with four statements:
include, require, include_once, require_once, let's take a look at the specific examples of PHP introduction files.
Basic syntax
require:the Require function is usually placed at the front of the PHP script, and before PHP executes, it is read into the require specified file, containing and attempting to execute the introduced script file. The way require works is to improve the efficiency of PHP execution, and when it is interpreted once on the same page, it will not be explained the second time. But again, because it does not repeat the introduction of the file, the include is needed when using a loop or conditional statement to introduce a file in PHP.
include: It can be placed anywhere in the PHP script, usually in the Process Control section. When a PHP script executes to a file introduced by the include specification, it is included and attempted to execute. This way you can simplify the process of executing the program. When the same file is encountered for the second time, PHP will be re-interpreted once, including the performance of the require is much less efficient, while in the introduction of the file contains user-defined functions, PHP in the interpretation process will occur in the function of duplicate definition problems.
require_once/include_once: the same as the require/include, the difference is that they will first check the target content is not imported before, if imported, Then it is not repeated to introduce the same content again.
different from each other
Include and require:
Include has a return value, and require does not return a value
Include generates a warning (e_warning) when the file fails to load, and the script continues to execute after the error occurs. So include is used when you want to continue execution and output results to the user.
Test1.php<?phpinclude './tsest.php '; Echo ' this is test1 ';? >//test2.php<?phpecho ' this was test2\n '; function test () { echo ' this is test\n ';}? >//Result: This is test1
Require generates a fatal error (E_COMPILE_ERROR) when the load fails, and the script stops executing after the error occurs. This is typically used when subsequent code depends on the loaded file.
Test1.php<?phprequire './tsest.php '; Echo ' this is test1 ';? >//test2.php<?phpecho ' this was test2\n '; function test () { echo ' this is test\n ';}? >
Results:
Include and include_once:
The file included in the include does not determine whether to repeat, as long as there is an include statement, it is loaded once (even if duplicate loading may occur). Include_once loading the file will have an internal judging mechanism to determine if the previous code has been loaded. It is important to note that Include_once is based on a file with the same path as before, and not based on the contents of the file (i.e., the contents of the two files to be introduced, the use of include_once or the introduction of two).
Test1.php<?phpinclude './test2.php '; Echo ' this is test1 '; include './test2.php ';? >//test2.php<?phpecho ' This is test2 ';? >//results: Test2this is test1this are test2//test1.php<?phpinclude './test2.php '; Echo ' this is test1 '; include_ Once './test2.php ';? >//test2.php<?phpecho ' This is test2 ';? >//results: This is Test2this was Test1//test1.php<?phpinclude_once './test2.php '; Echo ' this is test1 '; include '. Test2.php ';? >//test2.php<?phpecho ' This is test2 ';? >//results: This is test2this are Test1this is test2//test1.php<?phpinclude_once './test2.php '; Echo ' this is test1 '; Include_once './test2.php ';? >//test2.php<?phpecho ' This is test2 ';? >//Result: Test2this is test1
require and require_once: the same distinction as include and include_once.
Execution during onboarding
1. Exit PHP script mode from the include (Require) statement (enter HTML code mode)
2. Load the code in the file set by the include statement and try to execute the
3. Exit HTML mode, re-enter PHP script mode, continue the execution of the script program later
Test1.php
Results:
Analysis:
Path issues at load time
Relative path:
Locates a loaded file location relative to the location of the current Web page file.
./ represents the current location, which is the directory where the current Web page file resides./represents the previous level of the directory where the current Web page file is located//For example: include "./test2.php"; require ". /.. /test3.html ";
Absolute path:
Divided into local absolute path and network absolute path
Local absolute path:
From the local root directory recursively down the hierarchy until you find the file to be introduced in the corresponding directory.
Include "c:/php/test/test2.php";
As we all know, the absolute path is not conducive to the porting and maintainability of the project, so it is very seldom to write the absolute path directly in the code, but what if we need to use the absolute path?? There are magic constants __dir__ and global array $_server in PHP, using the following:
<?phpdefine (' DS ') or define (' DS ', directory_separator); echo "introduces (method i) using absolute paths; include __dir__. Ds. ' test2.php '; echo "using absolute path Loading method (method two)"; $root = $_server[' document_root '); Gets the root directory of the current site include $root. DS. ' Node_test '. DS. ' Inandre '. Ds. ' test2.php ';? >
Network absolute Path:
By linking to a file via the URL, the server will return the file that the URL points to after execution
Include "http://www.lishnli/index.php"
No path:
Just give the filename without giving the path information, PHP will find the file in the current page directory, if you find a file of the same name, execute and introduce.
Note: Regardless of the path, you must add the file suffix name, the four file loading method does not recognize the no suffix file.
Test1.phpinclude "./test2.php";//Results: This is Test2//test1.phpinclude "./test2";//Results:
Comparison of return values
As mentioned above , the include has a return value, and require has no return value
For include, if loading succeeds, there is a return value of 1, or false if loading fails.
For require, if the load succeeds, there is a return value of 1, and if loading fails, no return value.
test1.php<?php$a = include "./test2.php"; var_dump ($a); echo "<br>"; $b = include "./test2.phps"; Var_dump ($b) echo "<br>", $c = require "./test2.php"; Var_dump ($c); echo "<br>"; $d = require "./test2.phps"; Var_dump ($d);? >
Output:
When there is a return in the file:
When there is a return statement in the loaded file, there is another mechanism, at which point the return statement is to terminate the loading process, that is, the subsequent code that is loaded into the return statement in the file is no longer loaded. The return statement can also be used to return a data when loading a file.
test1.php<?php$a = include "./test2.php"; echo "<br>"; var_dump ($a);? >//test2.php//the file has a return statement <?php$b = ' test2 '; echo "loaded file: A position"; return $b; echo "<br loaded file: B position";? >
Results: