Include File Loading Sequence
The order of searching for contained files is:
First, search for the corresponding include_path in the current working directory,
Find the corresponding directory named include_path.
For example, include_path is ., the current working directory is/includetest/. The script must include an includetest/. php also has an include "B. php ", then look for B. the php sequence is/includetest/, and then/includetest/include /. If the file name starts with./or./, you can only search for it in the include_path directory of the current working directory.
The code structure is as follows:
~ /Public_html/includetest> ls-R
.:
A. php B. php include
./Include:
A. php B. php where the code of each file is:
~ /Public_html/includetest> cat a. php
<? Php
Set_include_path (".");
Include "include/a. php ";
Echo _ FILE _. PHP_EOL;
~ /Public_html/includetest> cat B. php
<? Php
Echo _ FILE _. PHP_EOL;
~ /Public_html/includetest> cat include/a. php
<? Php
Include "B. php ";
Echo _ FILE _. PHP_EOL;
~ /Public_html/includetest> cat include/B. php
<? Php
Echo _ FILE _. PHP_EOL; the output result is:
~ /Public_html/includetest> php a. php
# You can see include "B. php" in include/a. php; The includetest/B. php file is loaded first.
/Home/zhanhailiang/public_html/includetest/B. php
/Home/zhanhailiang/public_html/includetest/include/a. php
/Home/zhanhailiang/public_html/includetest/a. php Note: The concept difference between the working directory and the directory where the running script is located.
Include variable scope
When a file is included, the Code contained in the file inherits the variable range of the row where the include is located. From this point on, any variables available to the calling file in this row are also available in the called file. However, all functions and classes defined in the inclusion file have a global scope.
The Code is as follows:
~ /Public_html/includetest> cat c. php
<? Php
Include "d. php ";
Echo $ d. PHP_EOL;
E ();
~ /Public_html/includetest> cat d. php
<? Php
$ D = "hello world ";
Function e (){
Echo "hello world, e ()". PHP_EOL;
}
~ /Public_html/includetest> php c. php
Hello world
Hello world, e (). If include appears in a function in the calling file, all the Code contained in the called file will behave as they are defined within the function. So it will follow the variable range of the function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.
Include Code Execution
When a file is contained, the parser disconnects the target file from the PHP mode to the HTML mode and restores it to the end of the file. For this reason, any code that should be executed as PHP code in the target file must be included in the valid PHP start and end tags.
Include Remote File Processing
If "URL fopen wrappers" is activated in PHP (default configuration ), you can use URLs (HTTP or other supported encapsulation protocols-see supported protocols and encapsulation protocols for supported protocols) instead of local files to specify the files to be included. If the target server interprets the target file as PHP code, you can use the URL request string applicable to http get to pass variables to the included file. Strictly speaking, this is not the same as the variable space that contains a file and inherits the parent file. The script file is actually running on a remote server, and the local script includes the result.
Include processing Return Value
Processing the return value: You can use the return statement in the included file to terminate the execution of the program in the file and return the script that calls it. You can also return values from included files. The Return Value of the include call can be obtained just like that of a common function. However, this does not work when the remote file is included, unless the remote file output has a valid PHP start and end mark (like any local file ). You can define the required variable in the tag. The variable is available after the file is included.
Because include is a special language structure, its parameters do not need parentheses. Pay attention when comparing the returned values.
If the file cannot be contained, FALSE is returned and an E_WARNING warning is issued.
If a function is defined in the include file, these functions can be used independently before or after return in the main file. If the file is contained twice, PHP 5 issues a fatal error because the function has been defined, but PHP does not complain after return that the function has been defined. We recommend that you use include_once instead of checking whether the file is included and conditional return is included in the file.
Another way to "include" the PHP file to a variable is to use the output control function and include to capture its output.