php include() function test analysis_PHP tutorial

WBOY
Release: 2016-07-13 10:54:46
Original
1004 people have browsed it

Search under include_path, and 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 /www/, and the script needs to include an include/a.php tutorial and there is a sentence include "b.php" in the file, then the order of searching for b.php is /www/ first, then Then comes /www/include/. If the file name starts with ./ or ../ , it will only be searched under the include_path relative to the current working directory.

So the file structure looks like below

----a.php

----include/b.php

----include/c.php

where a.php

include 'include/b.php';

?>

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

b.php

include 'c.php';

include 'include/c.php';

?>

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

c.php

echo 'c.php';

?>

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

can all run correctly, indicating that two different include paths in b.php are feasible, and c.php can be found according to the include file search method.

But the best way is to use an absolute path. If an absolute path is used, the php kernel will load the file directly through the path without having to search for files one by one in the include path, which increases the efficiency of code execution

define('root_path',dirname(__file__));

include root_path.'/c.php';

?>


Conclusion:

Obviously, the format of the path behind the include and the PHP include path have an impact on program performance. The order 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 to include the file is the best choice, because then the PHP kernel will load the file directly through the path without having to search the include path for files one by one.

So we’d better define a constant of the absolute path of the project root directory in the public file of the project, and then put this constant in front of all include paths, so that all include in the project use absolute paths, both Improve program performance and reduce the trouble 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 them in large quantities, then please try to reduce the paths in the php include path to improve certain include performance. Because the fewer paths in the include path, the less time PHP will spend searching for files.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632306.htmlTechArticleSearch under include_path, and 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 /www/, and you need to include one in the script...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template