PHP keywords in PHP Include include, include_once, require, and require_once. In general, include and require are divided into one group, while include_once and require_once are a perfect form of improvement. By studying the nature of include and require and taking into account include_once and require_once, this article obtains the basic knowledge and potential problems contained in the PHP file and provides a feasible solution. Types of included files
The PHP parser processes all files containing keywords as PHP files.
That is, after the PHP parser reads the inclusion file of the current execution script, the PHP embedded content in the tag is executed as the source code, while other unincluded tags are directly output as text. There are two files a and B. php. The content is as follows:
A |
Akjfladskjfla <br> <? Echo $ _ server ['php _ Self '],' <br> '; echo' I am a <br> ';?> |
B. php |
<? Phpinclude 'a ';
Echo $ _ server ['php _ Self '],' <br> '; echo' I Am B <br> ';?> |
The output is as follows:
|
Akjfladskjfla/webapp/codesnipe/B. php I am /Webapp/codesnipe/B. php I am B |
File A has no suffix and contains a PHP embedded tag. If file a is executed, the system reports an error. But file B contains file. Text not included in the PHP embedded tag is directly output, and the text contained in the tag is executed as the PHP source code. The information format is still the experiment above. We delete file a and run file B. php. The error message is as follows:
|
Warning: Include (a) [function. Include]: failed to open stream: no such file or directory inC:/XAMPP/htdocs/webapp/codesnipe/B. phpOn Line3 Warning: Include () [function. Include]: Failed opening 'A 'for future Sion (include_path ='.; C:/XAMPP/PHP/pear/') inC:/XAMPP/htdocs/webapp/codesnipe/B. phpOn Line3 /Webapp/codesnipe/B. php I am B
|
The error message format is as follows:
Statement or function: failed to open stream: Specifies the row number of the file.
Difference between include and require
When files cannot be found, they are processed differently. Include will issue a warning, and then continue to execute; require will report an error and stop executing the current script.
The warning format given in the information format is include. The two warnings are followed by the output content of B. php. If you replace include with require, the information is as follows:
|
Warning: require (a) [function. Require]: failed to open stream: no such file or directory in C:/XAMPP/htdocs/webapp/codesnipe/B. php on line 3 Fatal error: require () [function. require]: Failed opening required 'A' (include_path = '.; c:/XAMPP/PHP/pear/') in C:/XAMPP/htdocs/webapp/codesnipe/B. PHP on line 3
|
It can be seen that their information is in the same format, and the second require information is changed to fatal error. The error message is not output after B. php is executed, because the script is exited after the require error is reported. The endless loop contained in the fileThe simplest form of an infinite loop is the form of two files: A. php and B. php. If a. php contains file B. php, and B. php contains file a. php, both a. php and B. php will enter an endless loop.
If you add a statement containing B. php In the first example, you can get a good example. The reason for the endless loop is easy to understand. I will not explain it here. Some people may think that such low-level errors will not be made during project development. When the project is small, it may be correct. But how can we ensure that there is no potential execution environment when a large software team works together? In addition, from the perspective of maintenance, this design is also unreasonable. Include_once and require_onceWhen include_once and require_once are executed for the first time, the result is the same as include and require. If the keyword contained in the file (the keyword in the same group) is not executed, it will be skipped.
Suppose there are three files: Indexa. php, indexb. php, and indexc. php. The source code is as follows:
Indexa. php |
<? Phpinclude_once 'indexb. php ';
Echo $ _ server ['php _ Self '],' <br> '; echo' I am a <br> ';?> |
Indexb. php |
<? Phpinclude_once 'indexa. php'; include_once 'indexc. php ';
Echo $ _ server ['php _ Self '],' <br> '; echo' I Am B <br> ';?> |
Indexc. php |
<? Phpinclude_once 'indexb. php'; include_once 'indexa. php ';
Echo $ _ server ['php _ Self '],' <br> '; echo' I am C <br> ';?> |
The output is as follows:
|
/Webapp/codesnipe/Indexa. php I am C /Webapp/codesnipe/Indexa. php I am B /Webapp/codesnipe/Indexa. php I am |
Run the Indexa. php file and read the source code of indexb. php when the containing statement contains indexb. php. The first sentence of the source code of indexb. php contains the file Indexa. php. Because the Indexa. php content already exists, the containing statement is invalid. The second contains indexc. php. The first sentence of the source code of indexc. php contains the file indexb. php. The content based on indexb. php already exists, so this statement is also invalid; the second statement is also invalid. Finally, the program execution result is shown above. Replace any include_once statement with require_once, and the program runs the same way. Therefore, there is no difference in handling errors in the same group. For better understanding, we can imagine that the PHP parser will create a source code source record table for each execution file. The record table records the complete path of the file contained in the source code. Include_once and require_once will check whether the new file is already in the record table each time they contain the new file. If the file already exists, the file content is not included; otherwise, the file path is recorded in the record table and its content is included. The difference from the previous group does not need to be said here. Include and require do not check the record table, which is the difference. In this way, we can easily solve potential endless loop problems and improve program reliability. In addition to being affected by the file inclusion relationship, the file contains the directory structure caused by the directory structure. The source code above contains the value of the variable $ _ server ['php _ Self. From the output, we can see that the variable value is the name of the currently running script, that is, directly running the requested PHP file rather than the included file. This fact shows that the source code of the contained file will be executed in the environment of the direct execution file, and it is inferior to the final file search path. A simple example. The project contains a folder named include. The following two files are indexb. php and indexc. php. The file Indexa. php is in the same directory as the include folder. The source code is as follows:
Indexa. php |
<? Phpinclude_once 'include/indexb. php ';
Echo $ _ server ['php _ Self '],' <br> '; echo' I am a <br> ';?> |
Indexb. php |
<? Phpinclude_once './indexc. php ';
Echo $ _ server ['php _ Self '],' <br> '; echo' I Am B <br> ';?> |
Indexc. php |
<? PHP
Echo $ _ server ['php _ Self '],' <br> '; echo' I am C <br> ';?> |
The output is as follows:
|
Warning: Include_once (./indexc. php) [function. Include-once]: failed to open stream: no such file or directory inC:/XAMPP/htdocs/webapp/codesnipe/include/indexb. phpOn Line3 Warning: Include_once () [function. Include]: Failed opening './indexc. php' for permission Sion (include_path ='.; C:/XAMPP/PHP/pear/') inC:/XAMPP/htdocs/webapp/codesnipe/include/indexb. phpOn Line3 /Webapp/codesnipe/Indexa. php I am B /Webapp/codesnipe/Indexa. php I am
|
The above output shows that indexb. ". /indexc. PHP ". "points to Indexa. the directory where PHP is located rather than the include directory itself, which leads to file search errors. If you change "./indexc. php" in indexb. php to "indexc. php", the script runs normally. This is because PHP itself has a default file search PATH value. But from the maintainability point of view, this kind of luck is not feasible. A solution because of the above problems, there will be a system configuration file during the project design, set the path value of the system in the configuration file, in this way, you can obtain the absolute path of the file to be included to avoid the above problems. For more information, see the next article.