【发布时间】:2012-04-18 22:36:12
【问题描述】:
我包含这个文件是为了收集其中一个变量。文件路径是正确的,我没有得到文件包含错误。但是当我尝试在该文件中打印一个变量时,它会给出未定义的变量错误。以下是代码。
include_once($path . "folder/file.php");
echo($code);
有一个php类。类里面有一个登录功能。在该函数中,我包含另一个文件,假设它是 funtions.php。
functions.php 中有上面的代码。
在 functions.php 文件中,我包含了这个文件,其中包含我正在寻找的变量(假设它是 test.php)。
test.php 看起来像这样(在 php 标签内)
$code="something";
$another_variable="something else";
所以现在就像我之前所说的,当我将它包含在 functions.php 中并打印 $code 时,为什么它会给出未定义的错误?
完整代码
function log($user, $pass) {
global $config;
$this->get_available_accounts();
if (isset($this->Users_obj[$user])) {
if ($this->Users_obj[$user]->userName == $user && $this->Users_obj[$user]->passWord == $pass) {
delete_cache_full();
$_SESSION['username'] = $user;
$_SESSION['log'] = true;
$_SESSION['usergroup']=$this->Users_obj[$user]->level;
$this->set_permission_session($_SESSION['usergroup']);
include_once $config->path . 'config.php';
$newUpdate2=showUpdates2();
if(!empty($newUpdate2)){
$_SESSION['updateremindlater']=$newUpdate2['date'];
}
//file
include_once $config->path . 'functions.php';
$func = new UserFuncs();
$func->validate();
include_once $config->path . 'view/ViewDashboard.php';
return true;
}
}
这就是包含这个文件的函数。 include_once $config->$path . 'functions.php'; 包含 functions.php 文件
functions.php 看起来像这样
include_once($path. "folder/config.php");
echo($code);
config.php 看起来像
$code = "ERGERW2342HV3453WERWEER";
$host_name = "SERV345WERFDGDDFGDGF";
$return_code = "DFGDFSDGFS";
$vurl = "YUIYUJHGJ";
$next_val ="AWERFDFVDV";
$val_exp = "NMGHJGJGH";
非常感谢您的帮助!
【问题讨论】:
-
您必须出示您的完整代码才能回答。您的范围错误,或者您正在取消设置变量或正在执行其他操作,我们无法从您显示的 sn-p 中确定。
-
我认为 deceze 是对的,因为您给出的描述很难理解,可能是您的理解有问题,无法轻松描述具体问题。所以最好的事情是你分享整个代码。或者画一个图像,这可能会更好:可视化包含是如何完成的。
include和include_once做不同的事情,所以要注意差异。 -
您能否发布
var_dump($config->path);的输出以检查它是否包含应包含的内容? :)
标签: php variables include undefined