Bird
0
0

What is the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - Array Functions
What is the output of the following PHP code?
$numbers = [1, 2, 3];
$result = array_map(fn($n) => $n * 2, $numbers);
print_r($result);
A[2, 4, 6]
B[1, 2, 3]
C[3, 4, 5]
DError: Invalid callback
Step-by-Step Solution
Solution:
  1. Step 1: Understand the callback function

    The callback doubles each number: $n * 2.
  2. Step 2: Apply the callback to each element

    1*2=2, 2*2=4, 3*2=6, so the new array is [2, 4, 6].
  3. Final Answer:

    [2, 4, 6] -> Option A
  4. Quick Check:

    Each element doubled = [2, 4, 6] [OK]
Quick Trick: Multiply each element by 2 in the callback [OK]
Common Mistakes:
  • Expecting original array unchanged
  • Confusing the callback operation
  • Thinking print_r outputs a string, not array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes