Hello everyone,
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.
Example:
$foo = array(1,2,3);
echo $foo[2]; // 3
echo $foo[-1]; // 3
$bar = 'baz';
echo $foo[2]; // 'z'
echo $foo[-1]; // 'z'
The change to PHP is quite trivial, but as it's a change to language it needs to be discussed.
I'm happy to open a RFC, I have got a wiki account but I haven't got permission to add a wiki page. Whom do I need to contact regarding getting correct permissions to add/edit wiki pages?
Kind Regards
Marc
Hello everyone,
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.
It would be convenient, but PHP already allows arrays to have negative
indices. Changing their behavior would be a major BC break.
Best Regards,
--Matthew
Hello everyone,
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.
Example:
echo $foo[2]; // 3
echo $foo[-1]; // 3
Negative indexes are used already - for negative indexes.
What should:
$foo = array(1,2,3, -1 => 4, 5, 6);
echo $foo[-1];
return? Now it returns 4.
$bar = 'baz';
echo $foo[2]; // 'z'
echo $foo[-1]; // 'z'The change to PHP is quite trivial, but as it's a change to language it needs to be discussed.
I'm happy to open a RFC, I have got a wiki account but I haven't got permission to add a wiki page. Whom do I need to contact regarding getting correct permissions to add/edit wiki pages?
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.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
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,
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.
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];
This seems simple enough for a hard-coded -1, but...
Would $var[-2] be strlen($var) - 2 and so on?
And then one would expect some rather complex logic to compute -N for
$var[-N]
At that point, this becomes a pretty big WTF, imho.
I've never honestly felt it to be a big burden to use strlen($var) -
1, so I don't really see the point, at least for me.
--
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE
Hi!
Can be:
$var = 'abc';
echo $var[-1];This seems simple enough for a hard-coded -1, but...
Would $var[-2] be strlen($var) - 2 and so on?
The main question is what happens with "foo"[-4] or ['x'][-2].
And then one would expect some rather complex logic to compute -N for
$var[-N]
I don't see much of complex logic here, but $a[2] = 'a' would create a
new array element if it does not exist, while $a[-2] can't. Not a big
issue, but somewhat inconsistent I guess.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
And then one would expect some rather complex logic to compute -N
for
$var[-N]I don't see much of complex logic here, but $a[2] = 'a' would create a
new array element if it does not exist, while $a[-2] can't. Not a big
issue, but somewhat inconsistent I guess.
$n = some_incredibly_long_and_complex_computation();
//and, for real fun, sometimes it returns positive, and sometimes
negative.
$s = $var[$n];
//a few hundred lines later, buried somewhere else
if ($n < 0){
//whatever
}
else{
//something entirely different
}
Sooner or later, somebody will do that, and I really don't want to
have to un-tangle all that mess to figure out if $n is positive or
negative for the sake of saving a few keystrokes...
--
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE
And then one would expect some rather complex logic to compute -N
for
$var[-N]
I don't see much of complex logic here, but $a[2] = 'a' would create a
new array element if it does not exist, while $a[-2] can't. Not a big
issue, but somewhat inconsistent I guess.
$n = some_incredibly_long_and_complex_computation();
//and, for real fun, sometimes it returns positive, and sometimes
negative.$s = $var[$n];
If $n is a negative value, anE_NOTICE
will be triggered. To not do
if ($n< 0){
//whatever
}
else{
//something entirely different
}
Prior to your string offset can be looked at being bad practice.
On Mon, Jun 11, 2012 at 12:13 PM, Stas Malyshev smalyshev@sugarcrm.comwrote:
Hi!
Can be:
$var = 'abc';
echo $var[-1];This seems simple enough for a hard-coded -1, but...
Would $var[-2] be strlen($var) - 2 and so on?
The main question is what happens with "foo"[-4] or ['x'][-2].
The same thing that currently happens for:
$f='foo'; echo $f[strlen($f)-4];
$a=['x']; $a[count($a)-2];
which appears to be notices:
Notice: Uninitialized string offset: -1 in - on line 2
Notice: Undefined offset: -1 in - on line 3
And then one would expect some rather complex logic to compute -N for
$var[-N]
I don't see much of complex logic here, but $a[2] = 'a' would create a
new array element if it does not exist, while $a[-2] can't. Not a big
issue, but somewhat inconsistent I guess.
Negative indices are currently valid for arrays. If anyone makes use of
negative indices, this could break their script. Personally, I'd rather
have Marc Easen's behavior.
Hi!
Can be:
$var = 'abc';
echo $var[-1];
This seems simple enough for a hard-coded -1, but...Would $var[-2] be strlen($var) - 2 and so on?
The main question is what happens with "foo"[-4] or ['x'][-2].And then one would expect some rather complex logic to compute -N for
$var[-N]
I don't see much of complex logic here, but $a[2] = 'a' would create a
new array element if it does not exist, while $a[-2] can't. Not a big
issue, but somewhat inconsistent I guess.
Please note I'm not referring the negative indexes in arrays, just
strings. As I see strings as being simpler to implement and produce a
greater benefit compared to supporting negative indexes for arrays. As
arrays are more complex structures and adding this behaviour would
complicate things somewhat.