Quick question ...

What happens if you use array_slice() and specify a length that is longer than the length of the array?

Sorry but I couldn't find the answer on PHP's doc pages or in a quick G search.

5 Years
Discussion Span
Last Post by Dani

Nothing strange - you just get all the items from the offset. Should work for both positive and negative offsets.

Making offset greater than the length will return an empty array


Sorry but I was having a dumb moment. Actually, wasn't talking about offsets. Was talking about the third parameter, which lets you specify how many elements you want from the offset. For whatever reason, I missed in the PHP docs that it said that if the length is longer than the array, it just works up until the end of the array without throwing any error message. Markin' this one solved.


Actually, wasn't talking about offsets

No I know, that's why I mentioned 'from the offset' and that it (oversized length) will work with positive and negative offsets. I just added the note of oversized offsets.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.