Test and analysis of php include () functions

Source: Internet
Author: User
Tags php tutorial relative

Under include_path, and then under include_path, where the current running script is located. For example, include_path Is ., the current working directory is/www/, and the script should include an include/. php Tutorial and include "B. php ", then look for B. the php sequence is/www/, and then/www/include /. If the file name starts with./or./, you can only search for it in the include_path Directory 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 ';

?>


Conclusion:

Obviously, the path format after include and the php include path have an impact on program performance. The sort of include performance from slow to fast is

Include 'A. Php' <include './a. Php' <include'/fullpath/a. php

In the code, using the absolute path include file is the best choice, because in this way, the php kernel directly loads files through the path instead of using the include path to search for files one by one.

So we 'd better define a constant for the absolute path of the project root directory in the public file of the project, and then carry this constant before all include paths, in this way, all the include operations in the project use absolute paths, which not only improves program performance, but also reduces the troubles caused by relative paths.

Reference Code (from emlog ):

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

Include emlog_root. '/config. Php ';

If you have used a large number of relative paths in the include 'test. Php' format in your project and it is not easy to modify a large number of paths, try to reduce the paths in the php include path to improve the include performance. Because the fewer paths in include path, the less time php will spend searching for files.

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.