Asked  2 Years ago    Answers:  5   Viewed   69 times

Is it possible to create a PHP function that takes a variable number of parameters all of them by reference?

It doesn't help me a function that receives by reference an array of values nor a function that takes its arguments wrapped in an object because I'm working on function composition and argument binding. Don't think about call-time pass-by-reference either. That thing shouldn't even exist.



PHP 5.6 introduced new variadic syntax which supports pass-by-reference. (thanks @outis for the update)

function foo(&...$args) {
    $args[0] = 'bar';

For PHP 5.5 or lower you can use the following trick:

function foo(&$param0 = null, &$param1 = null, &$param2 = null, &$param3 = null, &$param4 = null, &$param5 = null) {
  $argc = func_num_args();
  for ($i = 0; $i < $argc; $i++) {
    $name = 'param'.$i;
    $params[] = & $$name;
  // do something

The downside is that number of arguments is limited by the number of arguments defined (6 in the example snippet). but with the func_num_args() you could detect if more are needed.

Passing more than 7 parameters to a function is bad practice anyway ;)

Friday, September 23, 2022

The very last parameter, count, is passed by reference. You can see this in the description at where there's a & in front of the variable.

This means you cannot use a literal 1 there. You'd have to do:

$sql = str_replace('?', "'" . $param . "'", $sql, $count);
echo $count;

You'll now have displayed on the screen how many instances were replaced.

Thursday, September 1, 2022

After this loop is executed:

foreach ($x as $n => &$v) { }

$v ends up as a reference to $x[2]. Whatever you assign to $v actually gets assigned $x[2]. So at each iteration of the second loop:

foreach ($y as $n => $v) { }

$v (or should I say $x[2]) becomes:

  • 'bye bye'
  • 'world'
  • 'harsh'
Sunday, December 18, 2022

Let's pretend $x is a piece of paper with 5 written on it.

function sum($y) {
  $y = $y + 5; 

Here $y is the value of what you have written. You add 5 to such value in your mind, but the note is left untouched.

function sum(&$y) {
  $y = $y + 5; 

With the reference operator (&$y), you pass the very paper to the function, and it overwrites what's written on it.

For primitive values like numbers, I wouldn't bother and always return the value you want:

function valuePlusFive($x) {
  return $x + 5;

$x = 5;
$x = valuePlusFive($x);
Wednesday, September 7, 2022

There are no such subroutines in Fortran.

The syntax rule for a subroutine statement in Fortran 2008 is (, R1235):

[ prefix ] SUBROUTINE subroutine-name [ ( [ dummy-arg-list ] ) [ proc-language-binding-spec ] ]

where dummy-arg-list is a list (in assumed syntax rule terms) of dummy-args. A dummy-arg is (R1235) either a name or a literal *.

[Before we get too excited about the possibility of variadic support, the * refers, of course, to an alternate return indicator.]

A list (R101) still refers to a well-defined (at source time) number of items.

There is a stated restriction regarding interoperability with C, that (15.3.7) the C prototype

... does not have variable arguments as denoted by the ellipsis (...)

Similar arguments apply to (Fortran) functions.

Saturday, August 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages