A week or so ago I was fascinated by this Stack Overflow question: "How to changing value in $array2 without referring $array1?". It offers this code:
// baseline.php $array1 = array(1,20); $x = &$array1; $array2 = $array1; $array2 = 22; print_r($array1); // Output is 22
And this result:
In PHP, as the
=operator makes a copy of the variable being assigned, so one (and when I say "one", I mean "the person asking the question, and myself as well") might be surprised to see the answer is "
22", rather expecting it to be "
$array1is discrete from
$array1should not be impacted by the change to
$xis not a macguffin in this: remove that line, and things behave "as expected". How is making
$x- a reference to
$array1- somehow intertwined with