Apparently arrays in PHP are passed by value, not by reference as you would intuitively think. To illustrate, consider the following snippet of PHP code:

<?php
$a = array(1, 2, 3);
$b = $a;             //This actually creates a copy
$b[1] = 3;           //This does not affect $a
var_dump($a);
var_dump($b);
?>

When $b is assigned $a, PHP actually first creates a copy of the array $a, then assign the new array to $b. Essentially when $b is modified, $a is unaffected. The output of the code above follows:

array(3) { [0] => int(1) [1] => int(2) [2] => int(3) }
array(3) { [0] => int(1) [1] => int(3) [2] => int(3) }

I learned this the hard way and spent too much time figuring out what was wrong with my code.

Interesting and confusing enough, class instance objects are passed by reference.


http://stackoverflow.com/questions/2030906/are-arrays-in-php-passed-by-value-or-by-reference
http://stackoverflow.com/questions/2715026/are-php5-objects-passed-by-reference