所以基本上我无法完成的是回显一个变量,该变量包含包含文件中的变量。
解释:
我有3个文件:
index.php
system/functions.php
system/config.php
每个文件都包含另一个文件,因此它们的工作方式与类似(索引除外)
system/functions.php包含:
include_once $_SERVER['DOCUMENT_ROOT'].'/system/config.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/system/connection.php';
system/config.php包含:
include_once $_SERVER['DOCUMENT_ROOT'].'/system/functions.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/system/connection.php';
这没什么错,他们一起工作没有问题,他们可以成功地在自己之间传递变量但是
当我想:
echo $message;
在index.php上,输出仅为变量中的字符串,跳过所包含文件中的其他变量。。。
在这种情况下,functions.php文件包含
$message = $varfromconfig."Some String";
其中config.php包含
$varfromconfig = "someword ";
当我将$message
回显到索引页面时,只返回字符串Some String
,而不返回配置变量。。。为什么?(应返回someword Some String
)
谢谢。
当然,index.php同时包含配置和函数。。。
include_once 'system/config.php';
include_once 'system/connection.php';
include_once 'system/functions.php';
在index.php:上执行此操作
//include_once 'system/config.php'; remove this include from index.php
include_once 'system/connection.php';
include_once 'system/functions.php';
或,执行以下操作:
include_once 'system/config.php'; remove this include from index.php
include_once 'system/connection.php';
include 'system/functions.php'; //not include_once
当您同时插入这两个时,您就有了一个循环。我想config.php和functions.php之间的关系可能比您在这里写的更复杂,所以我会尝试详细说明发生了什么,以便您可以在需要时找到另一个解决方案。
在运行index.php之后,它会调用config.php。然后,在configuration.php config.php插入functions.php,但随后functions.php插入config.php和control.php再次插入1functions.php,这将永远保持下去。因此,php解释器只需在循环发生之前中断循环,只从config.php内部导入functions.php,但不要从functions.php内导入config.php 因此,现在我们刚刚通过了index.php的第1行,并首先从index.php导入了config.php,然后从configuration.php导入了函数.php
因此,首先从functions.php运行$message = $varfromconfig."Some String";
。一旦config.php还没有被解释,$varfromconfig仍然为空,它就会向$message
发送"Some String"。然后它运行$varfromconfig = "someword ";
,但没有将其分配给$message
,因为$message分配已经完成。
在第二行中,connections.php被导入,然后在第三行中,它尝试导入functions.php,但functions.php已经插入。所以include_once
不允许它被导入。
要解决此问题,一旦functions.php中的变量需要config.php首先运行,您只需在index.php处调用functions.php,然后让function.php自己调用config.php。另一种解决方案是使用include
而不是include_once
来插入函数.php,并让它被解释两次。