requestFirst passes a light copy of the argument array to requestSecond as you expect. But since the original argument contains a link to $conf , the copy also contains a link. Therefore, when you modify this particular element, the change is visible through all other references to the $conf variable:
function requestSecond($param) { $param['conf']++; // change to the int counter happens here } function requestFirst($params) { $params['conf']++; requestSecond($params); echo $params['conf']; // change is visible here } $conf = 1; requestFirst(array( 'conf' => &$conf, )); echo $conf; // change also visible here
However, $param itself is still a copy of $params , and any changes made to it will not be displayed outside the requestSecond :
function requestSecond($param) { $param['conf']++; $param['foo'] = 'bar'; } function requestFirst($params) { $params['conf']++; requestSecond($params); echo (int)isset($params['foo']);
You can even remove the link from the array after it is expanded; the change in the reference counter will remain, but the change in the array will not:
function requestSecond($param) { $param['conf']++; // remove the reference from the array -- this will only affect // the local copy $param and nothing else unset($param['conf']); } function requestFirst($params) { $params['conf']++; requestSecond($params); echo $params['conf']; // 3 }
source share