Re: Support negative indexes for arrays and strings
| From: | Marc Easen | Date: | Mon, 04 Jun 2012 19:08:58 +0000 |
| Subject: | Re: Support negative indexes for arrays and strings | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-60736@lists.php.net to get a copy of this message | ||
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.