Is php function pass by reference?

You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows:

Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference.

The following things can be passed by reference:

  • Variables, i.e. foo[$a]
  • References returned from functions, i.e.:

    See more about returning by reference.

No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid:

tnestved at yahoo dot com

7 years ago

By removing the ability to include the reference sign on function calls where pass-by-reference is incurred [I.e., function definition uses &], the readability of the code suffers, as one has to look at the function definition to know if the variable being passed is by-ref or not [I.e., potential to be modified].  If both function calls and function definitions require the reference sign [I.e., &], readability is improved, and it also lessens the potential of an inadvertent error in the code itself.  Going full on fatal error in 5.4.0 now forces everyone to have less readable code.  That is, does a function merely use the variable, or potentially modify it...now we have to find the function definition and physically look at it to know, whereas before we would know the intent immediately.

ccb_bc at hotmail dot com

3 years ago

mike at eastghost dot com

7 years ago

beware unset[]  destroys references

$x = 'x';
change[ $x ];
echo $x; // outputs "x" not "q23"  ---- remove the unset[] and output is "q23" not "x"

function change[ & $x ]
{
    unset[ $x ];
    $x = 'q23';
    return true;
}

tianyiw at vip dot qq dot com

1 year ago

I designed a class that can easily pass references.



My solution 👇

This is in no way a "bug" - the framework is performing as designed, but it took careful thought to figure out what was going on. PHP7.3

rob at librobert dot net

9 months ago

A reference remains a reference, even if it's an element of an array that is not passed by reference.

An example:



This will output the following:

array[3] {
  ["a"]=>
  int[1]
  ["b"]=>
  int[2]
  ["c"]=>
  &int[3]
}
array[3] {
  ["a"]=>
  int[4]
  ["b"]=>
  int[5]
  ["c"]=>
  &int[6]
}
array[3] {
  ["a"]=>
  int[1]
  ["b"]=>
  int[2]
  ["c"]=>
  &int[6]
}
int[6]

You could use this to allow a function to have read-write access to part of the array, while limiting it to read-only access for the rest of the array:

phpnet at holodyn dot com

8 years ago

The notes indicate that a function variable reference will receive a deprecated warning in the 5.3 series, however when calling the function via call_user_func the operation aborts without fatal error.

This is not a "bug" since it is not likely worth resolving, however should be noted in this documentation.

diabolos @t gmail dot com

10 years ago

Chủ Đề