Using references in PHP: The Basics
This is the first post of a three part series that will explain what the hell reference variables (pointers) are and you can use them in your PHP projects.
To quote the PHP Manual “References in PHP are a means to access the same variable content by different names.” which I believe is a perfect explanation. So, If you pass by reference instead of making a copy you are modifying the original variable. This is done by placing a single “&” (ampersand) in front of the variable you are assigning.
The concept sounds a little strange at first, but lets take a look at the example.
<?php // Create a variable. $original_variable = 1; // Create a reference to $original_variable $ref_variable =& $original_variable; // Increment $ref_variable by 10 $ref_variable += 10; // Output $original_variable echo $original_variable; // Outputs "11" // Decrement $original_variable by 5 $original_variable -= 5; // Output $ref_variable echo $ref_variable; // Outputs "6" ?>
I know what you are thinking… “what the hell?!” right? Ok, lets take that one a little slower.
// Create a variable. $original_variable = 1;
Just making avariable.
// Create a reference to $original_variable $ref_variable =& $original_variable;
$ref_variable is now a “clone” or reference of $original_variable. Any modifications made to either will reflect in both variables as they are essentially the same variable with different names. The one exception to this rule is deleting. If you delete $ref_variable, $original_variable will still exist.
// Increment $ref_variable by 10 $ref_variable += 10;
// Output $original_variable echo $original_variable; // Outputs "11"
This line will add 10 to $ref_variable which of course means the value of $original_variable will go up by 10.
// Increment $original_variable by 10 $original_variable -= 5; // Output $ref_variable echo $ref_variable; // Outputs "6"
Finally we have a similar block of code. The purpose of this was to demonstrate that you can modify either variable to change them both. Changes to the $original_variable will reflect in $ref_variable as soon as they are made.
One thing to remember is that you can only create references to variables. That means any static strings, ints, floats, etc will throw an error.
<?php // Try and create a new reference: $ref_variable =& 10; ?>
That code will throw a syntax error like “Parse error: syntax error, unexpected T_LNUMBER, expecting T_NEW or T_STRING or T_VARIABLE or ‘$’”, to correct it the value should first be assigned to a variable and then a reference made to that variable.
There are plenty more uses for references, i’ll explain how to use them in foreach loops in the next post.

This posts latest comments
I think a good way to visualize it for most people would be to compare it with something they are familiar with like shortcuts on the desktop.
Say you have 1 shortcut (alias) on your desktop that links to a folder on you harddisk. Now you copy the shortcut and make a duplicate of it.
Both the original and duplicate point to the same folder. Now you open the folder through clicking on the original shortcut and add a file to this folder.
Next when you go back to the desktop and click on the duplicate shortcut it will show you the same directory with that extra file.
But when you delete one shortcut, the folder is still on the disk and the other shortcut still points the folder. When you delete the value of the variable or in this case the foldercontent, it’s gone.