[PHP 8] `substr` returns empty string on offsets larger than string. It was `false` before

  • This topic has 5 replies, 1 voice, and was last updated 2 months ago by donatj.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2583
    ayeshrajans
    Participant

    [PHP 8] `substr` returns empty string on offsets larger than string. It was `false` before

    #2584
    [deleted]
    Guest

    [deleted]

    #2585
    jkoudys
    Guest

    I’m somewhat divided on this one. Limiting mixed-type returns is usually easier to reason about, and the `false` feels legacy as the standard mixed type for this kind of thing nowadays is a null. On the other hand, you’re losing useful information since substrings could be made with an equal start and end that would be empty, while if they were beyond the length before would’ve been false.

    #2586
    DaDeather
    Guest

    I’m quite irritated or am I missing something here?
    Look at this:
    https://3v4l.org/3H05i

    The only thing I can see has changed is the `grapheme_substr` but everything else is the same.

    #2588
    nikic
    Guest

    Worth noting is that this behavior is specific to substr(), to make certain usages painless (like “take (at most) the first N bytes of a string”). Pretty much everything else will throw ValueError on out of bounds offsets or lengths.

    #2589
    donatj
    Guest

    To me, blindly ignoring failures like this is the worst option. Seems like they should have just corrected the documentation but instead chose the nuclear option.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.