Since PHP 5, new
returns a reference automatically, so
using =& in this context is deprecated and
produces an E_DEPRECATED message in PHP 5.3 and
later, and an E_STRICT 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.)

Warning

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 $GLOBALS array.

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:

/* Assignment of array variables */$arr = array(1);$a =& $arr[0]; //$a and $arr[0] are in the same reference set$arr2 = $arr; //not an assignment-by-reference!$arr2[0]++;/* $a == 2, $arr == array(2) *//* The contents of $arr are changed even though it's not a reference! */?>

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:

<?phpfunction foo(&$var){$var++;}

$a=5;foo($a);?>

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

Return By Reference

User Contributed Notes 22 notes

I ran into something when using an expanded version of the example of pbaltz at NO_SPAM dot cs dot NO_SPAM dot wisc dot edu below.This could be somewhat confusing although it is perfectly clear if you have read the manual carfully. It makes the fact that references always point to the content of a variable perfectly clear (at least to me).

It appears that references can have side-effects. Below are two examples. Both are simply copying one array to another. In the second example, a reference is made to a value in the first array before the copy. In the first example the value at index 0 points to two separate memory locations. In the second example, the value at index 0 points to the same memory location.

I won't say this is a bug, because I don't know what the designed behavior of PHP is, but I don't think ANY developers would expect this behavior, so look out.

An example of where this could cause problems is if you do an array copy in a script and expect on type of behavior, but then later add a reference to a value in the array earlier in the script, and then find that the array copy behavior has unexpectedly changed.

About the example on array references.I think this should be written in the array chapter as well.Indeed if you are new to programming language in some way, you should beware that arrays are pointers to a vector of Byte(s).

<?php $arr = array(1); ?>$arr here contains a reference to which the array is located.Writing :<?php echo $arr[0]; ?>dereferences the array to access its very first element.

Now something that you should also be aware of (even you are not new to programming languages) is that PHP use references to contains the different values of an array. And that makes sense because the type of the elements of a PHP array can be different.

Something that might not be obvious on the first look:If you want to cycle through an array with references, you must not use a simple value assigning foreach control structure. You have to use an extended key-value assigning foreach or a for control structure.

A simple value assigning foreach control structure produces a copy of an object or value. The following code

When a variable is copied, a reference is used internally until the copy is modified. Therefore you shouldn't use references at all in your situation as it doesn't save any memory usage and increases the chance of logic bugs, as you discoved.

When there is a constructor, the strange behavior mentioned in my last post doesn't occur. My guess is that php was treating reftest() as a constructor (maybe because it was the first function?) and running it upon instantiation.

to reply to ' elrah [] polyptych [dot] com ', one thing to keep in mind is that array (or similar large data holders) are by default passed by reference. So the behaviour is not side effect. And for array copy and passing array inside function always done by 'pass by reference'...

In this example class name is different from its first function and however there is no construction function. In the end as you guess "a" and "c" are equal. So if there is no construction function at same time class and its first function names are the same, "a" and "c" doesn't equal forever. In my opinion php doesn't seek any function for the construction as long as their names differ from each others.

This appears to be the hidden behavior: When a class function has the same name as the class, it seems to be implicitly called when an object of the class is created.For instance, you may take a look at the naming of the function "reftest()": it is in the class "reftest". The behavior can be tested as follows:

<?phpclass reftest{ public $a = 1; public $c = 1;

public function reftest1() {$b =& $this->a;$b++; }

public function reftest2() {$d =& $this->c;$d++; }

public function reftest() { echo "REFTEST() called here!\n"; }}

$reference = new reftest();/*You must notice the above will also implicitly call reference->reftest()*/

An interesting if offbeat use for references: Creating an array with an arbitrary number of dimensions.

For example, a function that takes the result set from a database and produces a multidimensional array keyed according to one (or more) columns, which might be useful if you want your result set to be accessible in a hierarchial manner, or even if you just want your results keyed by the values of each row's primary/unique key fields.

<?phpfunction array_key_by($data, $keys, $dupl = false)/* * $data - Multidimensional array to be keyed * $keys - List containing the index/key(s) to use. * $dupl - How to handle rows containing the same values. TRUE stores it as an Array, FALSE overwrites the previous row. * * Returns a multidimensional array indexed by $keys, or NULL if error. * The number of dimensions is equal to the number of $keys provided (+1 if $dupl=TRUE). */ {// Sanity checkif (!is_array($data)) return null;

// Bind $last to one dimension 'deeper'. // First lap: was &$out, now &$out[...] // Second lap: was &$out[...], now &$out[...][...] // Third lap: was &$out[...][...], now &$out[...][...][...] // (etc.)$last =& $last[$value]; }

Something that has not been discussed so far is reference of a reference.I needed a quick and dirty method of aliasing incorrect naming until a proper rewrite could be done.Hope this saves someone else the time of testing since it was not covered in the Does/Are/Are Not pages.Far from best practice, but it worked.