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 = [1, 2, 3];
array_walk($numbers, function(&$value) {
    $value *= 2;
});
print_r($numbers);
AArray ( [0] => 2 [1] => 4 [2] => 6 )
BArray ( [0] => 1 [1] => 2 [2] => 3 )
CArray ( [0] => 0 [1] => 0 [2] => 0 )
DError: Cannot modify array elements
Step-by-Step Solution
Solution:
  1. Step 1: Understand the callback function

    The callback multiplies each value by 2, and the value is passed by reference (&$value), so changes affect the original array.
  2. Step 2: Apply changes to each element

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

    Array ( [0] => 2 [1] => 4 [2] => 6 ) -> Option A
  4. Quick Check:

    Reference callback doubles values [OK]
Quick Trick: Pass value by reference to modify array items [OK]
Common Mistakes:
  • Not using & to modify original array
  • Expecting original array unchanged
  • Confusing output format

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes