I have submitted a patch to support negative indexs in strings, as per the conversation adding them to arrays could possibly detract from the syntactical sugar they are indented to be.

In summary:

An alternative to:
$var = 'abc';
echo $var[strlen($var) - 1];

Can be:
$var = 'abc';
echo $var[-1];

Commit:
https://github.com/Easen/php-src/commit/7016e52677f525139491467f3e76862b9a597b76

Pull request:
https://github.com/php/php-src/pull/100

Thanks,


On 14/01/11 18:47, Stas Malyshev wrote:
Hi!

I would like to open the discussion around the support of negative
indexes, as I feel a lot of developers will benefit from this
syntactical sugar.

What you are looking for is implemented in array_slice(). Python has
shortcuts for this thing, like a[1:-1], but PHP doesn't. If you wanted
to RFC something here, I think the idea of $a[X:Y] with array_slice semantics could be doable. $a[-1:1] would be your case then.

Reply via email to