Hi François,

François Laupretre wrote:

Starting discussion about https://wiki.php.net/rfc/negative-string-offsets

Please read and comment.

I like this RFC. Being able to use negative offsets helps code readability, but it not being universally supported is annoying. I like that this brings more consistency.

I particularly like that this is supported for string indexing with the []/{} operator.

At present a negative offset is interpreted as being the maximum string length plus that offset, so $str[-1] is equivalent to $str[(2**64 - 1) - 1]. In practice, this means you will get an empty string and an "Uninitialized string offset:" E_NOTICE. It's not useful except for extremely large strings and so I suspect that behaviour comes from an implementation detail (naïve signed-to-unsigned integer conversion).

What you're proposing is to have the negative offset be interpreted as an offset from the end of the string, as the built-in functions do. This is more useful, but it's a change of behaviour from the present one, and thus a backwards-compatibility break, even if it's unlikely to affect real-world code. So, you might wish to mention that.

Also, I think the RFC would be more accessible if you included some examples of where to use negative offsets. That would give readers an at-a-glance view of what the RFC changes.

Anyway, I like what you're doing here. Thanks for the proposal!

--
Andrea Faulds
https://ajf.me/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to