Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - Array Functions
What will be the output of the following PHP code?
$numbers = [3, 1, 4, 1, 5];
sort($numbers);
print_r($numbers);
A[1, 1, 3, 4, 5]
BError: sort() is undefined
C[3, 1, 4, 1, 5]
D[5, 4, 3, 1, 1]
Step-by-Step Solution
Solution:
  1. Step 1: Understand what sort() does

    The sort() function sorts the array in ascending order, changing the original array.
  2. Step 2: Apply sort() to the array

    Sorting [3, 1, 4, 1, 5] ascending gives [1, 1, 3, 4, 5].
  3. Final Answer:

    [1, 1, 3, 4, 5] -> Option A
  4. Quick Check:

    sort() arranges numbers ascending = D [OK]
Quick Trick: sort() arranges array ascending by default [OK]
Common Mistakes:
  • Thinking sort() sorts descending by default
  • Expecting original array unchanged
  • Assuming sort() is not a built-in function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes