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.

Recommended Answers

All 4 Replies

Member Avatar for diafol

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.

Member Avatar for diafol

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.

LOL OK. Sorry, it's been a VERY long week as you can imagine. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.