0

I have a variable called

$variable

And when I call it inside a function then I need to use

some_function(){
global $variable;
echo $variable['array'];
}

But I dont want to use a global every time, Is there a way so by which I can call variable without setting a global everytime???

Thanks for your time.

4

3 回答 3

4

standard practice ...

some_function($variable){
echo $variable['array'];
}

called like any other function:

some_function($variable);
于 2013-04-04T03:32:47.563 回答
1

You can Pass it as a parameter to your function

$variable=array(2,5,6,9,7);

some_function($param){
  print_r($param);  // this is your variable
}

call it like

some_function($variable);
于 2013-04-04T03:33:24.207 回答
1

如果您不想使用“全局”,您可以使用 $GLOBALS 超全局变量:

function some_function() {
    echo $GLOBALS['variable']['array'];
}

$GLOBALS 是一个关联数组,包含对当前在脚本全局范围内定义的所有变量的引用。变量名是数组的键。

http://www.php.net/manual/en/reserved.variables.globals.php

于 2013-04-04T03:46:46.163 回答