Effective effect of copying php variables

Just wondering about the performance impact on copying very large php variables. For example, let's say $ arr is a huge array. If I do $ arr2 = $ arr, is it a deep copy or is $ arr2 just a pointer to $ arr, like Java? Thanks in advance.

+6
performance php
source share
3 answers

$arr2 = $arr creates a deep copy. But actual copying only happens when $ arr2 changes - PHP uses copy-on-write.

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

+6
source share

If you use $ arr2 = & $ arr;

It will refer to $ arr.

+1
source share

A general rule in PHP is not creating links unless you need the functionality they provide. Links will do the code otherwise.

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

+1
source share

All Articles