Store every project — even a simple letter — in its own folder or choose the correct project folder to put it in. No project stays simple for long, and you'll be glad to have everything in one place.
Articles > Webmaster > PHP References > A Visual Example
In the PHP language, variables never contain values directly. Instead, each variable refers to a value slot, and the value slot can contain any kind and amount of data. You can think of the variable as a filename and the value slot as the actual file contents — and just like in a filesystem, some slots contain enormous complicated data structures, while others simply contain a single integer or string.
The assignment operator (=) copies the value from one slot into another, while the assign reference operator (=&) changes the first variable so that it refers to the same slot as the second. To understand how these operators differ, consider the situation where you have four variables referring to two records:
$man = new Person("Fred",37);
$reference1 =& $man;
$woman = new Person("Wilma",35);
$reference2 =& $woman;
The first assignment copies the return value of the new Person() function into the value slot of $man.
Then the reference operator sets the $reference1 variable to refer to the same value slot. The slot that $reference1 previously referred to is dropped and the memory will be reclaimed by the garbage collector.
And we repeat for $woman and $reference2.
Note that neither $man nor $reference1 is the 'real' variable — both have 'equal rights' in the PHP language.

$man->name = "Barney"; $reference2->name = "Betty";
Because references have equal rights, it does not matter whether we use the reference variable or the original variable to access the record.
After assigning $man->name to "Barney", $reference1->name is also "Barney" — because $man and $reference1 refer to the same value slot, which holds the record formerly known as Fred.

$reference2 = $reference1; // COPIES the value in // $reference1 into the // value in $reference2, // which changes $woman
Here is where Java programmers need to pay attention!
The PHP assignment operator (=) copies the value slot of the first variable into the value slot of the second variable. It's exactly like copying one file onto another in the computer filesystem — so $woman->name is now "Barney" because the value slot referred to by $woman has been filled with data from $reference1.

$reference2 =& $reference1; // REASSIGNS $reference2 to
// refer to the same value
// slot that $reference1
// refers to
The reference operator (=&) does what the Java programmers are expecting. The variable $reference2 now refers to the same value slot that $reference1 refers to.

unset($reference2); $reference2 = "A String"; // After unsetting a variable // you can safely assign it // without accidentally // clobbering other data.
So how do we avoid clobbering data when we do variable assignmetns? The unset() function breaks the connection between a variable and its value slot. The variable is now undefined, and when you make an an assignment a new value slot will be created and the given value copied in.
Note that if you were to unset $woman, the second value slot would no longer have any variables referring to it, and the garbage collector will reclaim the memory.

$reference1 = 42; // Note that when you copy // a new value into a value // slot, the new value can // even be of a different // type! This can lead to // hard-to-find bugs...
Just in case you missed the significance of "Copying Values" earlier, here is a more extreme example.
When we copy the number 42 into the value slot referred to by $reference1, that slot changes from holding a record to holding a number!
This means, among other things, than any subsequent references to $man->name will now crash your script, since $man no longer refers to a record and so the -> operator is illegal.

See also:
Other advanced webmaster articles:
Other advanced webmaster resources: