Php Array Reference Assignment Definition

Passing 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. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.

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
mike at eastghost dot com
nickshanks at nickshanks dot com
fladnag at zerezo dot com
diabolos @t gmail dot com
fdelizy at unfreeze dot net
phpnet at holodyn dot com
Sergio Santana: ssantana at tlaloc dot imta dot mx
no at spam dot please
obscvresovl at NOSPAM dot hotmail dot com
pillepop2003 at yahoo dot de
pallsopp at gmail dot com

Assign By Reference

In the first of these, PHP references allow you to make two variables refer to the same content. Meaning, when you do:

it means that and point to the same content.


and are completely equal here. is not pointing to or vice versa. and are pointing to the same place.


If you assign, pass, or return an undefined variable by reference, it will get created.

Example #1 Using references with undefined variables

The same syntax can be used with functions that return references, and with the new operator (since PHP 4.0.4 and before PHP 5.0.0):

Since PHP 5, new returns a reference automatically, so using =& in this context is deprecated and produces an message in PHP 5.3 and later, and an message in earlier versions. As of PHP 7.0 it is syntactically invalid. (Technically, the difference is that, in PHP 5, object variables, much like resources, are a mere pointer to the actual object data, so these object references are not "references" in the same sense used before (aliases). For more information, see Objects and references.)

If you assign a reference to a variable declared global inside a function, the reference will be visible only inside the function. You can avoid this by using the array.

Example #2 Referencing global variables inside functions

Think about global $var; as a shortcut to $var =& $GLOBALS['var'];. Thus assigning another reference to $var only changes the local variable's reference.


If you assign a value to a variable with references in a foreach statement, the references are modified too.

Example #3 References and foreach statement

While not being strictly an assignment by reference, expressions created with the language construct array() can also behave as such by prefixing & to the array element to add. Example:

Note, however, that references inside arrays are potentially dangerous. Doing a normal (not by reference) assignment with a reference on the right side does not turn the left side into a reference, but references inside arrays are preserved in these normal assignments. This also applies to function calls where the array is passed by value. Example:

In other words, the reference behavior of arrays is defined in an element-by-element basis; the reference behavior of individual elements is dissociated from the reference status of the array container.

Pass By Reference

The second thing references do is to pass variables by reference. This is done by making a local variable in a function and a variable in the calling scope referencing the same content. Example:

will make to be 6. This happens because in the function the variable refers to the same content as . For more information on this, read the passing by reference section.

0 thoughts on “Php Array Reference Assignment Definition

Leave a Reply

Your email address will not be published. Required fields are marked *