Bird
0
0

Find the mistake in this code:

medium📝 Debug Q7 of 15
PHP - Array Functions
Find the mistake in this code:
$arr = [5, 6, 7, 8];
array_slice($arr, -2, 3);
print_r($arr);
Aarray_slice modifies the original array
Barray_slice returns a new array but original remains unchanged
CNegative offset is not allowed in array_slice
DSyntax error due to missing third parameter
Step-by-Step Solution
Solution:
  1. Step 1: Understand array_slice with negative offset

    Negative offset counts from the end; array_slice returns a new array.
  2. Step 2: Check if original array changes

    array_slice does not modify the original array, so print_r($arr) prints the original array unchanged.
  3. Final Answer:

    array_slice returns a new array but original remains unchanged -> Option B
  4. Quick Check:

    array_slice does not modify original array [OK]
Quick Trick: array_slice returns new array, original stays same [OK]
Common Mistakes:
  • Expecting array_slice to modify original
  • Thinking negative offset is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes