假设我有一段代码在启动时这样做:
if (!isset($_GLOBALS['something'])){
getAndWriteToGlobals($x, $y)
}
显然是getAndWriteToGlobals
函数,它做的事情很少,并将另一个数据放入$_GLOBALS['something'][$ee] = $value ->
,它正确地工作(我打印了整个$_GLOBALS['something']
)与print_r
,一切都在那里。
我遇到的问题是,当程序从这个函数返回时,我试图在另一个变量中获得数组,如
$var = $_GLOBALS['something'];
$var在本例中打印时不包含任何内容(甚至不是null), count($var)
返回0
。我遗漏了什么?
谢谢!
编辑:function getAndWriteToGlobals($hostname,$community,$oidIndex, $oidValue) {
$indexes = snmprealwalk($hostname, $community, $oidIndex);
$values = snmprealwalk($hostname, $community, $oidValue);
if (empty($indexes)) {
print "Empty indexes array!n";
}
else{
$c=0;
$a = array();
foreach($indexes as $key => $indxVal){
if (strpos($indxVal,'word') !== false) {
preg_match("/[0-9]+$/", $key, $matches);
$ind = $matches[0];
$a[$c] = $ind;
$c++;
}
}
$i=0;
foreach($values as $key => $value){
if (strpos($key, $a[$i]) !== false) {
preg_match("/[+-0-9]+$/", $value, $matches);
$value = $matches[0];
$_GLOBALS['something'][$a[$i]] = $value;
$i++;
}
}
$_GLOBALS
或$GLOBALS
?
// in somewhere (i think very first of master include file)
global $_GLOBALS;
$_GLOBALS['foo'] = 123;
// more code...
// and in function(s)
function getAndWriteToGlobals(...
global $_GLOBALS;
// do stuff with $_GLOBALS
PS:我不建议将变量命名为PHP结构样式。我更喜欢将这些变量命名为$_globals
或$globals
($GLOBALS['globals']
, $GLOBALS['cfg']
等)。