No. You can not. The prameter declaration passed to ref is explicitly specified as function something(&$arg1, &$arg2) . If you do not know the number of parameters at compile time, you can do something like this:
function something_else($args) { foreach ($args as $arg) $GLOBALS[$arg] *= 2; } $a = 1; $b = 3; something_else(array('a', 'b')); echo $a . $b;
Basically, the code passes functions to the parameter names that the function will change. $GLOBALS contains references to all defined variables in the global scope of the script. This means that if the call is made from another function, it will not work:
function something_else($args) { foreach ($args as $arg) $GLOBALS[$arg] *= 2; } function other_function(){ $a = 1; $b = 3; something_else(array('a', 'b')); echo $a . $b;
triggers notifications undefined indices a and b . Thus, another approach is to create an array with references to variables that the function will change as:
function something_else($args) { foreach ($args as &$arg) $arg *= 2; } function other_fucntion(){ $a = 1; $b = 3; something_else(array(&$a, &$b)); echo $a . $b;
Pay attention to & on the foreach line. This is necessary so as not to create a new variable, iterating over the array. PHP> 5 required for this function.
source share