①Function and usage
Can reduce code duplication
include(_once) ("path to file") and require(_once) ("path to file")
②Understanding
To put it bluntly, it means replacing the include(_once) and require(_once) lines
with the content in the included file ③Note
include/require The included files must be added with because when including, the first thing to understand is that the file content is an ordinary string. When encountering php ?> tag is used to explain
④Path
You can use absolute paths or relative paths; both forward and backward slashes are acceptable under Windows. Linux only recognizes forward slashes, so it is best to use forward slashes
⑤Difference
include means inclusion. When the file cannot be found, a warning will be reported. error, and then the program continues to execute
require means necessary. When the file cannot be found, a fatal error will be reported, and the program will stop executing
After adding once , the system will judge, if it is already included, it will not include it for the second time
eg: There is an a.php file whose content is
The content in the b.php file is $a=5; require_once("a.php"); echo $a; require_once("a.php"); echo $a;
The first result The output is 6, and the second output is still 6, explain. . _once is only included once. If once is not added, the second output will be 7
⑥Choose
For example, it is the system configuration. If it is missing, the website will not allow it. To run, naturally use require. If it is a certain statistical program, if it is missing, it will only count the number of people on the website. It is not necessary. You can use include
. The difference in efficiency is whether to add once or not. On once, although the system considers loading only once for you, the system's judgment will be that the efficiency will be reduced. Therefore, you should adjust the directory structure at the beginning of development and try not to use _once.
⑦Special Usage
Use include/require to return the return value of the included page
In a.php page: ..... return $value; In the b.php page: $v = include("a.php");
This is used when configuring the website I'll meet you occasionally!