Avoid Replication with the PHP Reference Operator
Since PHP version 4+ PHP has offered us a way to reduce memory usage in our programs when assigning a variable to another variable. Normally when you assign the value of one variable to another separate variable, the variable that is getting assigned to another is then duplicated by PHP in memory to be used in the operation.
What this means for us as programmers is that if we want to take the value of one variable and assign it to another, we must fist allow PHP to duplicate the variable so that it may be assigned to another.
The reference operator must be used in conjunction with the assignment operator.
We can circumvent this activity by using what is called the reference operator "&". Here is an example:
<?php $a = 10; $b = $a; //$a is duplicated in the background by PHP so that it can be assigned to $b // $b is no 10 //avoid duplicating the assigned variable using the reference operator "&" $a = 10; $b = &$a; //& used in conjunction with the assignment operator $a = 15; //$a and $b = 15 and there is only one $a ?>
The way this works is that even after $b is assigned "&$a" later in the script when $a changes so does $b because the variable that was assigned to $b is not a copy, but the value of $a no matter where is its as long as it is after the assignment using the reference operator.
Basically, $b = &$a means, that $b references the value of $a from this point forward in your script, so if $a changes below your reference, then the value of the referenced variable changes with the new assignment of $a as long as it happens below the referenced call in your script.