What is the significance of passing arrays by reference in PHP functions, and how does it affect the behavior of sorting methods?
Passing arrays by reference in PHP functions allows the function to directly modify the original array, rather than creating a copy of it. This can be more memory efficient and can lead to better performance, especially when dealing with large arrays. When using sorting methods like `sort()` or `rsort()`, passing the array by reference ensures that the original array is sorted in place, rather than returning a sorted copy.
<?php
function sortArray(&$arr) {
sort($arr);
}
$numbers = [3, 1, 2];
sortArray($numbers);
print_r($numbers); // Output: [1, 2, 3]
?>