Php Variable Assignment By Reference

It's not deprecated and is unlikely to be. It's the standard way to, for example, make part of one array or object mirror changes made to another, instead of copying the existing data.

It's called assignment by reference, which, to quote the manual, "means that both variables end up pointing at the same data, and nothing is copied anywhere".

The only thing that is deprecated with is "assigning the result of by reference" in PHP 5, which might be the source of any confusion. is automatically assigned by reference, so is redundant/deprecated in, but not in .


Since it's hard to search, note that (equals ampersand) is the same as (equals space ampersand) and is often written such that it runs into the other variable like or (ampersand dollar sign variable name). Example simplified from the docs:


Here's a handy link to a detailed section on Assign By Reference in the PHP manual. That page is part of a series on references - it's worth taking a minute to read the whole series.

Objects and references

One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

Example #1 References and Objects

The above example will output:

miklcct at gmail dot com
Anonymous
wassimamal121 at hotmail dot com
Aaron Bond
kristof at viewranger dot com
mjung at poczta dot onet dot pl
Ivan Bertona
Hayley Watson
Anonymous
Jon Whitener
cesoid at gmail dot com
wbcarts at juno dot com
Rob Marscher
lazybones_senior
akam at akameng dot com

0 thoughts on “Php Variable Assignment By Reference

Leave a Reply

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