No. You cannot. The declaration of a prameter passed by ref is explicit as function something(&$arg1, &$arg2). If you don't 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; //returns "26"
Basically the code passes to the function the names of the params the function will modify. $GLOBALS holds references to all defined variables in the global scope of the script. This means if the call is 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; //returns "13"
}
other_function();
triggers notices undefined indexes a and b. So another approach is to create an array with references to the variables the function will modify 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; //returns "26"
}
other_fucntion();
Note the & on the foreach line. It is needed so not to create a new variable iterating over the array. PHP > 5 needed for this feature.