What is the difference between array_splice and array_slice in PHP?

The main difference between array_splice and array_slice in PHP is in their functionality. - array_splice is used to remove a portion of an array and replace it with new elements, while array_slice is used to extract a portion of an array without modifying the original array. - array_splice modifies the original array, while array_slice does not. - If you want to remove elements from an array and replace them with new elements, use array_splice. If you simply want to extract a portion of an array without modifying the original array, use array_slice.

// Using array_splice to remove elements from an array and replace them with new elements
$array = [1, 2, 3, 4, 5];
$removed = array_splice($array, 1, 2, [6, 7]);
print_r($array); // Output: [1, 6, 7, 4, 5]

// Using array_slice to extract a portion of an array without modifying the original array
$array = [1, 2, 3, 4, 5];
$slice = array_slice($array, 1, 2);
print_r($slice); // Output: [2, 3]
print_r($array); // Output: [1, 2, 3, 4, 5]