【发布时间】:2011-09-19 19:49:08
【问题描述】:
我在理解 PHP 中的包含和函数作用域时遇到了一些麻烦,并且在谷歌上搜索了一下也没有提供成功的结果。但是问题来了:
这不起作用:
function include_a(){
// Just imagine some complicated code
if(isset($_SESSION['language'])){
include 'left_side2.php';
} else {
include 'left_side.php';
}
// More complicated code to determine which file to include
}
function b() {
include_a();
print_r($lang); // Will say that $lang is undefined
}
所以本质上,在left_side.php 和left_side2.php 中有一个名为$lang 的数组。我想在b() 中访问它,但是上面的代码设置会说$lang 是未定义的。但是,当我在b() 的开头复制并粘贴include_a() 中的确切代码时,它会正常工作。但正如你可以想象的那样,我不希望将代码复制并粘贴到我需要的每个函数中。
如何缓解这个范围问题,我做错了什么?
【问题讨论】:
标签: php function include scope