It’s Not True that PHP Arrays are Copied by Value

PHP, Arrays & Passing by Reference

Do you know that objects in PHP5 are passed by reference, while arrays and other scalar variables are passed by value? Yes, you know it, but it’s not exactly true. Let’s see some example and let’s try to answer few questions.

// depending on the machine but both lines return
// expectedly equal values: 331208
 
// 331208
echo memory_get_usage();
echo memory_get_usage();

These two lines of code, expectedly return the same value (in my case 331208), which shows us that because nothing happened in between them the memory usage isn’t growing. Let’s now put some code in between them.

echo memory_get_usage(); // 331616
$a = 10;
echo memory_get_usage(); // 331696

Now because of the variable $a, we get a little more memory consuption! The same thing (with even more memory usage) happens if we have an array initialization.

echo memory_get_usage(); // 332128
$a = array(1, 2, 3, 'hello', 'world');
echo memory_get_usage(); // 332728

OK, now we see how much memory PHP is using for this very simple and small array. If we copy this array, we’d expect PHP to take twice as much memory, but that’s not the case!

echo memory_get_usage(); // 332336
$a = array(1, 2, 3, 'hello', 'world');
$b = $a;
echo memory_get_usage(); // 332984

Actually if we had $b = 10; this will consume more!!! memory than the code above.

echo memory_get_usage(); // 332336
$a = array(1, 2, 3, 'hello', 'world');
$b = 10;
echo memory_get_usage(); // 333016

This is simply because in the first case $b wasn’t a copy of $a, while in the second case we have a brand new variable on the ground, which, of course, requires memory.

Why Arrays aren’t Copied?

Actually now we see that copying by reference and by value is absolutely the same.

$a = array(1, 2, 3, 'hello', 'world');
 
// this line is exactly the same as ...
$b = $a;
 
// this line
$b = &$a;

That is because in both case the array is passed by reference. It is copied once we make changes to $b. Then in the first case $b becomes a copy of $a and it’s changed, while in the second case $b is exactly the same array as $a and every change to $b changes $a as well.

echo memory_get_usage();
$a = array(1, 2, 3, 'hello', 'world');
echo memory_get_usage();
$b = $a;
$b = array(1, 2, 3, 'goodby', 'world');
echo memory_get_usage();

Conclusion

If we take an example from Zend Framework, where often we work with arrays that are passed to the view as a “copy”:

$a = array(1, 2, 3, 'hello', 'world');
$this->view->a = $a; // this is NOT a copy

This will not consume more memory!!! The only way to make your application more memory inefficient is to change directly the $this->view->a array, so be careful!

3 thoughts on “It’s Not True that PHP Arrays are Copied by Value

  1. Hi friend

    thanks for sharing these.. why don’t you add some social network sharing widget for your blog

  2. Thanks for this post. Very interesting. I’d never have guessed that arrays are only copied when one of the referencing variables is changed (if it was not explicitly declared to be a reference with =&, in which case the references would remain the same). Up to this moment, in case it happens, they will reference the same array no matter if I use = or =&

    Even though in practice this is transparent to the user, it’s very good to know. Thanks for letting us know it!

  3. Arrays are copied in PHP or?

    $arr = array(1, 2, 3, 4, 5);
    
    $b = $arr;
    
    print_r($arr);
    
    echo "";
    
    print_r($b);
    
    echo "";
    
    $b[0] = "A";
    
    print_r($arr);
    
    echo "";
    
    print_r($b);
    
    echo "";
    
    $c =&$arr;
    
    print_r($arr);
    
    echo "";
    
    print_r($c);
    
    $c[0] = "A";
    
    echo "";
    
    $c =&$arr;
    
    print_r($arr);
    
    echo "";
    
    print_r($c);
    

Leave a Reply

Your email address will not be published. Required fields are marked *