PHP passes all objects by reference. (PHP5?)
PHP passes all arrays by value.
Initially, PHP passed both objects and arrays by value, but in order to reduce the number of created objects, they switch objects to automatically pass by reference.
There is actually no logical reason why PHP does not pass arrays by reference, but that is how the language works. If you need to, you can iterate over arrays by value, but you must explicitly declare the value by reference:
foreach ( $myArray as &$val ){ $val = 1;
Thanks to Yacoby for an example.
Frankly, I prefer arrays to be passed by value, because arrays are a type of basic data structure, and objects are more complex data structures. The current system makes sense, at least for me.
source share