When you assign-by-value an array variable in PHP,

2019-09-20 13:36发布

问题:

It seems really inefficient that in PHP, when an array is assigned by value all of its internal elements are recursively copied to the new variable. Is this what really happens internally?

回答1:

No, the internally array is not deep-copied on assignment. Consider the following snippet:

$a = array(111, 222, 333);
$b = $a;
$b[0] = 999;

If a picture is worth a thousand words, then here is what happens internally when arrays are assigned and then their array elements are modified: