I am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a string.
Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break BC.
Ilia
Ilia Alshanetsky wrote:
I am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break
BC.
I need this, +1
Greg
Ilia Alshanetsky wrote:
I am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break
BC.I need this, +1
How can you need this, you can use substr()
just fine.
Derick
--
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org
Derick Rethans wrote:
Ilia Alshanetsky wrote:
I am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break
BC.I need this, +1
How can you need this, you can use
substr()
just fine.
I don't need PHP either. any other questions?
Greg
Derick Rethans wrote:
How can you need this, you can use
substr()
just fine.
Almost no peace of functionality in PHP in unique, some extensions
duplicate core/standard functionality and vice versa. Usually the "core"
offers a simpler access to a resource and full pledged extensions
gives the full capabilities. The best example of this is streams,
sockets extension and cURL.
On a similar note, most users don't use even 1/2 the functionality PHP
offers, does it mean we should remove those parts of the language or not
have add them in the 1st place?
Just because substr()
or $a[strlen($a) - 1] would work, it does not
mean they are the most optimal approaches. The change to permit negative
string offsets would require @ most 2-3 line change in Zend and it would
be trivial to write a series of tests to cover the affected code. We are
not talking about a major change here.
Ilia
Ilia Alshanetsky wrote:
Derick Rethans wrote:
How can you need this, you can use
substr()
just fine.Almost no peace of functionality in PHP in unique, some extensions
duplicate core/standard functionality and vice versa. Usually the "core"
offers a simpler access to a resource and full pledged extensions gives
the full capabilities. The best example of this is streams, sockets
extension and cURL.On a similar note, most users don't use even 1/2 the functionality PHP
offers, does it mean we should remove those parts of the language or not
have add them in the 1st place?Just because
substr()
or $a[strlen($a) - 1] would work, it does not
mean they are the most optimal approaches. The change to permit negative
string offsets would require @ most 2-3 line change in Zend and it would
be trivial to write a series of tests to cover the affected code. We are
not talking about a major change here.Ilia
Privet Ilia,
Andy mentioned that he tried in the past to separate {} and [] but after
some unsuccess he left the idea of separating. So, the patch definitely
won't be 2-3 lines of code.
There are a lot of people which don't know about negative offsets at all,
and use strlen()
for calculations. Moving this feature to the language
itself won't help them that much if just don't read the documentation (
as they do).
Andrey
Andrey Hristov wrote:
Andy mentioned that he tried in the past to separate {} and [] but after
some unsuccess he left the idea of separating. So, the patch definitely
won't be 2-3 lines of code.
I am well aware that at this point PHP has no separation between [] and
{} as far as string offsets go. My initial suggestion was to add the
support for negative offsets and not to limit it to a particular
construct. Given that this is intended for strings only, I readily
agreed with people who suggested that it should be available only for
{}, assuming there was a separation.
There are a lot of people which don't know about negative offsets at all,
and usestrlen()
for calculations. Moving this feature to the language
itself won't help them that much if just don't read the documentation (
as they do).
Almost any new function/construct/extension begins from obscurity. Even
people who know about it won't use it till enough PHP versions have been
released with it, because they'd up limit their apps to "latest and
greatest".
Ilia
Ilia Alshanetsky wrote:
I am well aware that at this point PHP has no separation between [] and
{} as far as string offsets go. My initial suggestion was to add the
support for negative offsets and not to limit it to a particular
construct. Given that this is intended for strings only, I readily
agreed with people who suggested that it should be available only for
{}, assuming there was a separation.
What happens to arrays when there's no difference between [] and {} ?
$array[-1]
$string[-1]
This will confused people because $array[-1] actually wouldn't return the last element (it could, if we know the indices are only positive integers, but we can't assume when that when using arrays with arbitrary keys -> hashes).
That seems problematic to me.
- Markus
why not add it with the {} operators then?
-sterling
I am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break BC.Ilia
Sure, I have no operator preference.
Ilia
Sterling Hughes wrote:
why not add it with the {} operators then?
-sterling
I am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break BC.Ilia
Right, I was about to say that too. If we only did it for the {} operator
it would encourage people to use that little-used operator thereby
removing a source of confusion and at the same time the BC issues are
minimal.
-Rasmus
why not add it with the {} operators then?
-sterling
I am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break BC.Ilia
Exactly what I thought when reading the original post :)
- Michael
why not add it with the {} operators then?
-sterling
On Fri, 29 Oct 2004 15:07:05 -0400, Ilia Alshanetsky ilia@prohost.org
wrote:I am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a
string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break
BC.Ilia
why not add it with the {} operators then?
Yeah, [] is for arrays, {} for string indexes though I still think this
should not be in, one can use substring for this. Something like a
"negative string index" is IMO too much magic.
Derick
--
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org
why not add it with the {} operators then?
Yeah, [] is for arrays, {} for string indexes though I still think this
should not be in, one can use substring for this. Something like a
"negative string index" is IMO too much magic.
Just as much magic as the negative start in substr()
function. It would
be consistent with substr()
precedent of negative indexes counting
backward from end of string. Also, everyone knows functions run slower
than operators :)
Cheers,
Rob.
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Yeah, [] is for arrays, {} for string indexes though I still think this
should not be in, one can use substring for this. Something like a
"negative string index" is IMO too much magic.Just as much magic as the negative start in
substr()
function. It would
be consistent withsubstr()
precedent of negative indexes counting
backward from end of string.
But substr()
is a function, that's totally different from a language
construct which should be a generic tool, not a specific one like Ilia
is proposing here.
Also, everyone knows functions run slower than operators :)
coughbullshit argumentcough
Derick
--
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org
Yeah, [] is for arrays, {} for string indexes though I still think this
should not be in, one can use substring for this. Something like a
"negative string index" is IMO too much magic.Just as much magic as the negative start in
substr()
function. It would
be consistent withsubstr()
precedent of negative indexes counting
backward from end of string.But
substr()
is a function, that's totally different from a language
construct which should be a generic tool, not a specific one like Ilia
is proposing here.Also, everyone knows functions run slower than operators :)
coughbullshit argumentcough
Well for the amount of times you need to traverse backward in a string
it probably is a crap argument :) However it does indeed stand that
operators are processed faster than functions I would wager due to the
overhead of setting up parameters etc. Contrast is_null( $x ) versus $x
=== null, there's about 17% difference in speed. Similarly (0.0 + $x)
takes almost half the time of floatval( $x ).
Cheers,
Rob.
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
----- Original Message -----
From: "Ilia Alshanetsky" ilia@prohost.org
To: internals@lists.php.net
Sent: Friday, October 29, 2004 3:07 PM
Subject: [PHP-DEV] Negative string offset support
I am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break
BC.
this, coupled with splicing, is very useful in python. i believe it would be
very useful in php as well. agree with sterling's suggestion - it would
enable all of the functionality, rather than some.
paul
We haven't yet separated the semantics of {} and []. It was always planned
but never done.
I'll put it on my TODO and hopefully will find some time to look into it.
Andi
At 04:13 PM 10/29/2004 -0400, Paul G wrote:
----- Original Message -----
From: "Ilia Alshanetsky" ilia@prohost.org
To: internals@lists.php.net
Sent: Friday, October 29, 2004 3:07 PM
Subject: [PHP-DEV] Negative string offset supportI am wondering what are people's opinions on adding support for negative
string offsets that could be used to access data from the end of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would break
BC.this, coupled with splicing, is very useful in python. i believe it would be
very useful in php as well. agree with sterling's suggestion - it would
enable all of the functionality, rather than some.paul
echo $a{strlen($a)-1}; ? is that really that bad.
I do worry that at present
$a = "a string";
$p = 0
while( $p < strlen($p) )
.. do stuff that could do $p-- or $p++ ....
echo $a{$p};
}
at present that would produce a nice error if you went < 0.. easy to
spot.. - if -ve was supported it could do unexpected stuff..
Regards
Alan
Ilia Alshanetsky wrote:
I am wondering what are people's opinions on adding support for
negative string offsets that could be used to access data from the end
of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would
break BC.Ilia
While you are correct that invalid offset in a string will produce an
error message. This error message is E_NOTICE, given that our default
INI error reporting level won't display those it's arguable that very
few people will see them and consequently do something about it.
echo $a{strlen($a)-1}; ? is that really that bad.
It is not that bad, but it could be better. Basic tests I've ran show
that using a strlen()
is 30-40% slower and substr()
is 20-25% slower
then $a{-1}.
Ilia
Alan Knowles wrote:
I do worry that at present
$a = "a string";
$p = 0
while( $p < strlen($p) )
.. do stuff that could do $p-- or $p++ ....
echo $a{$p};
}at present that would produce a nice error if you went < 0.. easy to
spot.. - if -ve was supported it could do unexpected stuff..Regards
AlanIlia Alshanetsky wrote:
I am wondering what are people's opinions on adding support for
negative string offsets that could be used to access data from the end
of a string.Ex. $a = "123"; echo $a[-1]; // would print 3
I don't think we should do this for arrays, since -1 and similar are
valid array keys, which means adding this support for arrays would
break BC.Ilia