Bird
0
0

Given $arr = [1, 2, 3];, which code snippet correctly moves the last element to the front using array_pop and array_unshift?

hard📝 Application Q9 of 15
PHP - Arrays
Given $arr = [1, 2, 3];, which code snippet correctly moves the last element to the front using array_pop and array_unshift?
Aarray_pop($arr); array_unshift($arr, 3);
B$last = array_pop($arr); array_unshift($arr, $last);
C$last = array_pop(); array_unshift($arr, $last);
Darray_unshift($arr, array_pop());
Step-by-Step Solution
Solution:
  1. Step 1: Use array_pop correctly

    array_pop($arr) removes last element and returns it, stored in $last.
  2. Step 2: Use array_unshift to add at front

    array_unshift($arr, $last) adds $last at the beginning of $arr.
  3. Final Answer:

    $last = array_pop($arr); array_unshift($arr, $last); -> Option B
  4. Quick Check:

    Pop last, then unshift to front [OK]
Quick Trick: Pop last element, then unshift it to front [OK]
Common Mistakes:
  • Calling array_pop without argument
  • Using array_pop without storing result
  • Passing wrong arguments to array_unshift

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes