[PHP] include detail_php tutorial

Source: Internet
Author: User
[PHP] include details. The order in which include files are loaded is: first, find the file in the relative include_path of the current working directory, and then the order in which inclu include files are loaded in the directory where the current running script is located.
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

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 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

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 (). 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.

The sequence of searching for include files is as follows: First, search for the corresponding include_path in the current working directory, and then find the inclu in the directory where the current running script is located...

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.