Consider the functions sort
and array_reverse
.
Why does one modify the variable passed, whereas the other return a new version?
$a = array(3, 1, 2);
sort($a);
// $a === array(1, 2, 3)
array_reverse($a);
// $a === array(1, 2, 3)
sort
could just as easily been written to return a modified copy of the argument, and vice-versa for array_reverse
.
The reason I'm asking is because I want to know if there's any guidelines for deciding whether write functions using the "pass-by-reference and modify" approach vs the "pass-by-value, modify and return" approach.