Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
<?php
$nums = [3, 1, 2];
$result = sort($nums);
var_dump($result);
echo implode(' ', $nums), "\n";bool(true) 1 2 3
- Expecting
$numsto stay3 1 2because an array parameter is a copy:sortdeclares its parameter by reference, so it reorders the caller's array itself. - Expecting
sortto return the sorted array: it returns onlytrue, so$sorted = sort($a)leavestruein$sortedand the new order in$a.
- How would you sort a list and keep its original order in another variable?
- Which function sorts by value but keeps each key with its value?