Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Arrays
What will be the output of this PHP code?
$arr = [];
array_push($arr, 1, 2, 3);
print_r($arr);
AArray ( [1] => 1 [2] => 2 [3] => 3 )
BArray ( [0] => 1 [1] => 2 [2] => 3 )
CArray ( [0] => Array (1, 2, 3) )
DArray ( [0] => 3 )
Step-by-Step Solution
Solution:
  1. Step 1: Understand array_push with multiple values

    array_push adds all values 1, 2, 3 to the end of $arr.
  2. Step 2: Check print_r output

    print_r shows array with keys 0,1,2 and values 1,2,3 respectively.
  3. Final Answer:

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

    array_push adds all values individually [OK]
Quick Trick: array_push can add multiple elements at once [OK]
Common Mistakes:
  • Thinking array_push nests values as sub-array
  • Expecting keys to start at 1
  • Assuming only last value is added

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes