1

I see many code like this:

function load_items(&$items_arr) {
    // ... some code
}

load_items($item_arr);
$v = &$items_arr[$id];

compared to code as follow:

function load_items() {
    // ... some code
    return $items_arr;
}

$item_arr = load_items();
$v = $items_arr[$id];

Did the second code will copy items_arr and $item_arr[$id]? Will the first code import performance?

roger
  • 9,063
  • 20
  • 72
  • 119
  • 6
    .......... what? – dbf Mar 01 '16 at 10:05
  • @dbf I just hear from others, I think this is incredible – roger Mar 01 '16 at 10:06
  • 2
    Your question is not very clear, to say the least. By pre-pending the variable with ampersand `&` you are passing it to a function by reference, the variable needs to be defined first, then modified by your function – B-and-P Mar 01 '16 at 10:08
  • Um no it's not really incredible, I just don't understand a think your asking here. Your pointing to a thing called [copy by value or copy by reference](http://php.net/manual/en/language.references.pass.php). Read about it and how PHP deals with it .. – dbf Mar 01 '16 at 10:09
  • @dbf I want to make sure one thing, if `$items_arr` is very very big, the second code will run very slow? Because if I write similar code in python, I do not need to worry about this. – roger Mar 01 '16 at 10:12
  • can you even return an array in PHP? – Cârnăciov Mar 01 '16 at 10:13
  • @B-and-P I want to make sure one thing, if I do not want to make my program slow, should I always use `&`? – roger Mar 01 '16 at 10:14
  • @aron9forever No you can't, it's a down-side that comes with PHP, you need jQuery to solve that problem :P .. jQuery solves basically everything PHP can't .. – dbf Mar 01 '16 at 10:14
  • 1
    @aron9forever - no, you can return anything since the language basically copied `C`, but they didn't let it return an array, only the most common data structure and the very reason PHP is popular. `` – Mjh Mar 01 '16 at 10:14
  • 1
    @roger - no, **do not** always use `&`. You won't make your program faster or slower by doing this, but you risk running into unexpected behavior. PHP is smart enough not to pass an entire array by value to a function, you don't have to use `&` for that. This copying will occur when **modifying** data you have passed, except for objects. [Read this answer please](http://stackoverflow.com/questions/628938/what-is-copy-on-write) and don't do silly things like you wanted for no valid reason. – Mjh Mar 01 '16 at 10:22

1 Answers1

2

No, it will not copy the value right away. Copy-on-write is one of the memory management methods used in PHP. It ensures that memory isn’t wasted when you copy values between variables. What that means is that when you assign:

$v = $items_arr[$id];

PHP will simply update the symbol table to indicate that $v points to the same memory address of $item_arr[$id], just if you change $item_arr or $v afterwards then PHP allocates more memory and then performs the copy.

By delaying this extra memory allocation and the copying PHP saves time and memory in some cases.

There's are nice article about memory management in PHP: http://hengrui-li.blogspot.no/2011/08/php-copy-on-write-how-php-manages.html

William J.
  • 1,574
  • 15
  • 26