开发者

Performance impact of copying php variables

开发者 https://www.devze.com 2022-12-22 12:11 出处:网络
Just wondering about the performance impact of copying very large p开发者_如何学JAVAhp variables. For example say $arr is an enormous array. If I do $arr2 = $arr, is this a deep copy or is $arr2 merel

Just wondering about the performance impact of copying very large p开发者_如何学JAVAhp 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.


$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.


If you use $arr2 = &$arr ;

It will reference of the $arr .


The general rule in PHP is don't create references unless you need the functionality they provide. References will only make the code slower otherwise.

http://www.php.net/manual/en/language.references.php

0

精彩评论

暂无评论...
验证码 换一张
取 消