Home  >  Article  >  Backend Development  >  [PHP]include detailed explanation_PHP tutorial

[PHP]include detailed explanation_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:09:58919browse

include loading file order
The order to look for included files is:

First search under include_path relative to the current working directory,
Then search under include_path relative to the directory where the currently running script is located.
For example, include_path is ., the current working directory is /includetest/, the script needs to include an includetest/a.php and there is an include "b.php" in the file, then the order of searching for b.php is first /includetest/, then is/includetest/include/. If the file name starts with ./ or ../, it will only be searched under the include_path relative to 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 each file code is:

~/public_html/includetest> cat a.php

set_include_path(".");
include "include/a.php";

echo __FILE__ .PHP_EOL;
~/public_html/includetest> cat b.php

echo __FILE__ .PHP_EOL;
~/public_html/includetest> cat include/a.php

include "b.php";
echo __FILE__ .PHP_EOL;
~/public_html/includetest> cat include/b.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 will be 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 conceptual difference between the working directory and the directory where the script is run.

include variable scope
When a file is included, the code contained in it inherits the variable scope of the include line. From that point on, any variables available in the calling file at that line are also available in the called file. However, all functions and classes defined in include files have global scope.

The code is as follows:

~/public_html/includetest> cat c.php

include "d.php";
echo $d.PHP_EOL;
e();
~/public_html/includetest> cat d.php

$d = "hello world";

function e() {
echo "hello world, e()".PHP_EOL;
}
~/public_html/includetest> php c.php
hello world
hello world, e() Correspondingly, if include appears in a function in the calling file, all code contained in the called file will behave as if they were defined inside the function. So it will follow the variable scope of that 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 included, the parser leaves PHP mode and enters HTML mode at the beginning of the target file, and resumes at the end of the file. For this reason, any code in an object file that should be executed as PHP code must be included within valid PHP opening and closing tags.

include handles remote files
If "URL fopen wrappers" are enabled in PHP (the default configuration), it is possible to specify URLs (via HTTP or other supported wrapping protocols - see Supported Protocols and Wrapping Protocols for supported protocols) instead of local files. Contained files. If the target server interprets the target file as PHP code, you can pass variables to the included file using the URL request string for HTTP GET. This is not strictly the same thing as containing a file and inheriting the variable space of the parent file; the script file has actually been run on the remote server, and the local script includes its results.

include processing return value
Handling return values: You can use the return statement in an included file to terminate the execution of the program in the file and return to the script that called it. It is also possible to return values ​​from included files. The return value of the include call can be obtained like a normal function. This does not work when including a remote file, however, unless the remote file's output has legal PHP start and end tags (like any local file). You can define the required variables within the tag, which will be available after the location where the file is included.

Because include is a special language construct, its parameters do not require parentheses. Be careful when comparing their return values.

If the file cannot be included, return FALSE and issue an E_WARNING warning.

If there are functions defined in include files, these functions can be used in the main file independently of whether they are used before or after return. If the file is included twice, PHP 5 issues a fatal error because the function is already defined, but PHP does not complain after return that the function is already defined. It is recommended to use include_once instead of checking if a file is included and returning it conditionally in the included file.

Another way to "include" a PHP file into a variable is to use an output control function in conjunction with include to capture its output.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477600.htmlTechArticleinclude loading file sequence The order in which to search for include files is: First, search under the include_path relative to the current working directory, and then It is the inclu relative to the directory where the currently running script is located...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn