6

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.

jhchen
  • 14,355
  • 14
  • 63
  • 91
  • 1
    *(reference)* Copy on Write in the PHP language: http://www.research.ibm.com/trl/people/mich/pub/200901_popl2009phpsem.pdf – Gordon Mar 05 '10 at 08:10

3 Answers3

6

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

Frank Farmer
  • 38,246
  • 12
  • 71
  • 89
1

If you use $arr2 = &$arr ;

It will reference of the $arr .

Pavunkumar
  • 5,147
  • 14
  • 43
  • 69
1

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

Matthew
  • 47,584
  • 11
  • 86
  • 98