[Php]include Detailed _php Tutorial

Source: Internet
Author: User
Tags variable scope
Include Load file order
The order in which to look for include files is:

First, in the current working directory of the relative include_path under the search,
Then look under the include_path of the directory where the script is currently running.
For example Include_path is., the current working directory is/includetest/, in the script to include a includetest/a.php and in the file has an include "b.php", the search b.php order first/in cludetest/, and then/includetest/include/. If the file name is./or: /start, search only under the include_path 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 Each of the file codes 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 is:

~/public_html/includetest> PHP a.php
# you can see the include "b.php" in include/a.php; 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 working directory differs from the concept of the directory where the script is run.

Include variable scope
When a file is contained, the code contained in it inherits the scope of the variable for the row in which the include is located. From there, any variables that are available to the calling file at that line are also available in the called file. However, all functions and classes defined in the include file 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 range of the function. An exception to this rule is Magic constants which is evaluated by the parser before the include occurs.

Include Code execution
When a file is included, the parser goes out of PHP mode at the beginning of the target file and enters HTML mode to resume at the end of the file. For this reason, any code in the target file that should be executed as PHP code must be included in the valid PHP start and end tags.

Include Handling remote files
If "url fopen wrappers" is activated in PHP (the default configuration), you can specify the files to be included by URL (see supported protocols and encapsulation protocols via HTTP or other supported encapsulation protocols) rather than local files. If the destination server interprets the destination file as PHP code, you can pass the variable to the included file with the URL request string for the HTTP GET. Strictly speaking, this is not the same as a variable space that contains a file and inherits the parent file; The script file is actually already running on the remote server, and the local script includes its results.

Include handling return values
Process 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 called it. It is also possible to return a value from a contained file. The return value of the include call can be obtained just like a normal function. However, this does not work with remote files unless the remote file's output has a valid PHP start and end tag (as with any local file). You can define the desired variable within the tag, which is available after the file is contained.

Because include is a special language structure, its parameters do not require parentheses. Be careful when comparing the return value.

Returns FALSE and emits a e_warning warning if the file cannot be included.

If a function is defined in the include file, these functions can be independent of whether it is used in the main file before or after the return. If the file is included two times, PHP 5 makes a fatal error because the function is already defined, but PHP does not complain after the return that the function has been defined. It is recommended to use include_once instead of checking to see if the file is included and conditionally returned in the include file.

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

http://www.bkjia.com/PHPjc/477600.html www.bkjia.com true http://www.bkjia.com/PHPjc/477600.html techarticle The Include load file sequence looks for the include file in the order that the current working directory is relative to the include_path, and then the current running script is located in the relative inclu of the directory ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.