1

Example

$value1 = $array[ 0 ][ $key ];
$value2 = (string) $array[ 0 ][ $key ];

Variables are read-only, I dont modify value of them. Does PHP copies value of those elements or makes reference?

EDIT:
Copying of value takes time and memory. I would like to know if copying is performed? For example - in functions PHP doesnt copy value unless you modify it

EDIT2:
https://stackoverflow.com/a/3845530/1398264
I'm not sure if this applies to the assignment.

Community
  • 1
  • 1
l00k
  • 1,525
  • 1
  • 19
  • 29
  • @AbraCadaver copying of value takes time and memory. I would like to know if copying is performed? For example - in functions PHP doesnt copy value unless you modify it – l00k Oct 26 '16 at 21:26
  • 1
    PHP uses copy-on-write. It tries to avoid physically copying anything unless it needs to. – Shira Oct 26 '16 at 21:40
  • @ShiraNai7 what about casting - does it counts like a write? Please add your answer so I can make it approved. – l00k Oct 26 '16 at 21:50

2 Answers2

2

PHP uses copy-on-write. It attempts to avoid physically copying data unless it needs to.

From PHP docs - Introduction to Variables:

PHP is a dynamic, loosely typed language, that uses copy-on-write and reference counting.

You can test this easily:

/* memory usage helpers */

$mem_initial = memory_get_usage();
$mem_last = $mem_initial;

$mem_debug = function () use ($mem_initial, &$mem_last) {
    $mem_current = memory_get_usage();
    $mem_change = $mem_current - $mem_last;
    echo 'Memory usage change: ', $mem_change >= 0 ? '+' : '-', $mem_change, " bytes\n";
    $mem_last = $mem_current;
};

/* test */

echo "Allocating 10kB string\n";
$string = str_repeat('x', 10000);
$mem_debug();
echo "\n";

echo "Copying string by direct assignment\n";
$string2 = $string;
$mem_debug();
echo "\n";

echo "Modyfing copied string\n";
$string2 .= 'x';
$mem_debug();
echo "\n";

echo "Copying string with a (string) cast\n";
$string3 = (string) $string;
$mem_debug();

Output for PHP 5.x:

Allocating 10kB string
Memory usage change: +10816 bytes

Copying string by direct assignment
Memory usage change: +56 bytes

Modyfing copied string
Memory usage change: +10048 bytes

Copying string with a (string) cast
Memory usage change: +10104 bytes
  • direct assignment doesn't copy the string in memory as expected
  • modifying the copied string does duplicate the string in memory - copy-on-write has happened
  • assigning the string with an additional (string) cast seems to duplicate the string in memory even if it is unchanged

Output for PHP 7.0:

Allocating 10kB string
Memory usage change: +13040 bytes

Copying string by direct assignment
Memory usage change: +0 bytes

Modyfing copied string
Memory usage change: +12288 bytes

Copying string with a (string) cast
Memory usage change: +0 bytes
  • copy-on-write behavior is the same as in the 5.x versions but meaningless (string) casts don't cause the string to be duplicated in memory
Shira
  • 6,392
  • 2
  • 25
  • 27
-1

Yes, PHP copy value from $array[ 0 ][ $key ] to variable $value1 ( Your code: $value1 = $array[ 0 ][ $key ];).
In PHP for reference used reserved symbol &, and you can work with reference like in other programing language. For using reference you need to modify you code: $value1 = & $array[ 0 ][ $key ]; Use unset($value1) for remove reference from variable to array.

  • http://stackoverflow.com/questions/11074970/will-copy-on-write-prevent-data-duplication-on-arrays As mentioned in this topic - PHP uses COW mechanism. However I dont know does casting makes changes – l00k Oct 26 '16 at 21:48
  • I think that you can't get reference to auto cast value. If you work with strict types you can use function or cast value in any expression. function string_cast – Petro Sydor Oct 26 '16 at 21:52
  • Sorry wrong answer before, i cant edit it. Use some thing like: $variable1=&$var2; and then use it with casting like: (string)$variable1; – Petro Sydor Oct 26 '16 at 21:54
  • I would like to avoid reference cuz of performence aspects. References are always slower then copying value. – l00k Oct 26 '16 at 21:57
  • I can't say exactly some thing about you code behavior, because i don't see your code. I often use reference when i have huge array of object and need to do with them something, often change value inside object with function. Without reference you will have 2 copy operation. First when you value copy to function scope, second when you want write changed value to variable.With reference you can modify directly, and it is faster with huge data. – Petro Sydor Oct 26 '16 at 22:12