Question : Passing arrays by value

I understand that PHP natively passes variables by value, not by reference (unless instructions are passed to do so, e.g., using the ampersand operator).

So if I have a function that returns an array after processing it several times, am I getting reduced performance (or just using poor practice), by doing something like this:

function process_list($list){
  $list = array_slice($list, $from, $len);
  $list = some_sort_function($list, $sorts);
  $list = some_filter_function($list, $filters);
  return $list;
}

Am I creating several wasted duplicates using the above?  I should mention that $list isn't just one array - that example function above might be used to process several different datasets, that might or might not have anything in common.  It's psuedo-code, but I think represents the fundamental question better than the more length real-world situation.

TYIA

Answer : Passing arrays by value

In this case, all you are saving is one copy operation of the original array.
The last 2 lines are why passing by value is normally preferred, after

$some_dataset = // big complicated array
$processed = process_list($some_dataset);

You normally end up with 2 arrays, $some_dataset and $processed, both of which are different.
If process_list takes an array by reference, then those two lines actually end up with $some_dataset and $processed being 2 COPIES of the same arrays (not reference, copy by value).
Array assignments are always copy-by-value, unless & is used to copy the reference.

array_slice by itself creates a copy of the subset.
The 2 sort functions also return new arrays.
You are probably better off just sticking to passing by values unless you really understand it.

FWIW, to save one single copy operation, the last 2 lines would be

$some_dataset = // big complicated array
process_list($some_dataset); // and continue to use $some_dataset after this line
Random Solutions  
 
programming4us programming4us