Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass unlimited arguments by reference in php [duplicate]

I have php function that has an unlimited number of args which I am getting from func_get_args(). I have some operations with arguments (changing string or doing something) and I want this to be like a passing argument by reference. is it possible?

example:

$test = 'foo';
$test2 = 'bar';

function test(){
    $args = func_get_args();
    foreach($args as $arg)
        $arg .= 'baz';
}

test($test, $test2);
like image 923
bzzb Avatar asked Aug 11 '11 13:08

bzzb


1 Answers

Since PHP-5.6 you can use a variadic reference:

function test(&...$args) {
    foreach ($args as &$arg) {
        $arg .= 'baz';
    }
}
like image 78
Markus Malkusch Avatar answered Oct 08 '22 21:10

Markus Malkusch