Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance impact of copying php variables

Just wondering about the performance impact of copying very large php variables. For example say $arr is an enormous array. If I do $arr2 = $arr, is this a deep copy or is $arr2 merely a pointer to $arr like it is in Java? Thanks in advance.

like image 451
jhchen Avatar asked Mar 05 '10 06:03

jhchen


1 Answers

$arr2 = $arr creates a deep copy. But the actual copying only happens when $arr2 is modified -- PHP utilizes copy-on-write.

If you want a "pointer" instead of a copy, use $arr2 =& $arr, which makes $arr2 a reference to $arr.

like image 185
Frank Farmer Avatar answered Sep 19 '22 22:09

Frank Farmer