简体   繁体   English

了解php包括路径

[英]Understanding php include paths

I'm trying to understand exactly how php treats include paths on page calls. 我试图准确理解php如何处理包含页面调用的路径。

Lets say I have index.php in the base directory, functions/functionFile1.php, functions/functionFile2.php and settings/settingFile.php 可以说我在基本目录中有index.php,functions / functionFile1.php,functions / functionFile2.php和settings / settingFile.php

So the straightforward one is index.php has 所以直截了当的是index.php

include_once("functions/functionsFile1.php"); 
include_once('settings/settingsFile.php');

In the functions1 file: 在functions1文件中:

include_once('settings/settingsFile.php');
include_once('functionsFile2.php);

function DoX()
{
   include("settings/settings.php"); 
   $variableInSettingsPage; 
} 

How is it that this works? 这是如何工作的? The functions1.php file seems to be able to call both files relative to itself (functions2.php) and its calling page (settings/settings.php)? functions1.php文件似乎能够调用相对于它自己的两个文件(functions2.php)及其调用页面(settings / settings.php)? And the function that requires calling the settings file can use a path relative to the calling page, even though it's out of scope and will refuse to use a path relative to its page? 并且需要调用设置文件的函数可以使用相对于调用页面的路径,即使它超出范围并拒绝使用相对于其页面的路径?

Php use an include path, every path from which a file can be included is stored there. Php使用包含路径,可以包含文件的每个路径都存储在那里。 When you try to include a file, php will loop through every path and check if your file is in one of them, if it is, it will stop looping and include it, if it is not, it will keep looping until it eventually reach the end and raise an error. 当你试图包含一个文件时,php将循环遍历每个路径并检查你的文件是否在其中一个,如果是,它将停止循环并包含它,如果不是,它将保持循环直到它最终到达结束并引发错误。

You can check what's in your include_path, using get_include_path() 您可以使用get_include_path()检查include_path中的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM