Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q4 of 15
PHP - Array Functions
What will be the output of the following PHP code?
$arr = [1, 2, 3, 4, 5];
$removed = array_splice($arr, 1, 2);
print_r($removed);
AArray ( [0] => 1 [1] => 2 )
BArray ( [0] => 4 [1] => 5 )
CArray ( [0] => 3 [1] => 4 )
DArray ( [0] => 2 [1] => 3 )
Step-by-Step Solution
Solution:
  1. Step 1: Understand array_splice parameters

    array_splice removes 2 elements starting at index 1, which are elements 2 and 3.
  2. Step 2: Determine removed elements

    The removed elements are [2, 3], so print_r($removed) outputs Array ( [0] => 2 [1] => 3 ).
  3. Final Answer:

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

    array_splice removes elements = Array ( [0] => 2 [1] => 3 ) [OK]
Quick Trick: array_splice returns removed elements [OK]
Common Mistakes:
  • Confusing removed elements with remaining array
  • Wrong start index usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes