I know that prepending a '&' to your PHP variable sets up a reference to the original variable instead of copying its value like so:

$original = 'apples';
$secondary = &$original;
$original = 'oranges';

echo $secondary; // 'oranges'

If it works this way, why not just use the original variable then?


This might help: When do I use Pass by Reference in PHP?

Written by s992

Point of interest: Internally, every duplicate of a variable in PHP is actually a reference, until you try and change its contents. Its then copied and modified at that point.

Written by berty

Accepted Answer

$original = 'apples';

function foo($word) {
    $word = 'oranges';

echo $original; // apples, because only local $word was changed, not $original.

echo $original; // oranges, because $original and $word are the same
Written by Coronatus
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki