[Confused] include file inclusion path search in PHP

Source: Internet
Author: User

First, let's take a look at the description of the include file search principles in the PHP official manual:

Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E. g. If your include_path isLibraries, Current working directory is/Www/, You have DEDInclude/a. phpAnd there isInclude "B. php"In that file,B. phpIs first looked in/Www/libraries/And then in/Www/include/. If filename begins./Or../, It is looked only in the current working directory.

Find the file inclusion sequence first in the relative include_path of the current working directory, and then in the relative include_path of the directory where the current running script is located. For example, include_path is., The current working directory is/Www/, The script must includeInclude/a. phpAnd there is a sentence in this fileInclude "B. php", Then lookB. phpFirst/Www/And then/Www/include/. If the file name is./Or../First, it is only found in the include_path of the current working directory.

So the file structure is as follows:

---- A. php

---- Include/B. php

---- Include/C. php

A. php

<? PHP

Include 'include/B. php ';

?>

-----------------------

B. php

<? PHP

Include 'C. php ';

Include 'include/C. php ';

?>

--------------------------

C. php

<? PHP

Echo 'C. php ';

?>

--------------------------

Both of them can run correctly, which means that the two different inclusion paths in B. php are feasible. You can find C. php In the method of searching for inclusion files based on include.

However, the best way is to use absolute paths. If absolute paths are used, the PHP kernel will directly load files through paths without having to include paths to search for files one by one, increasing the code execution efficiency.

<? PHP

Define ('root _ path', dirname (_ file __));

Include root_path. '/C. php ';

?>

For more information about the execution performance of different file inclusion methods, see this 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.