Re: Negative string offset support
| From: | Ilia Alshanetsky | Date: | Sat, 30 Oct 2004 06:20:08 +0000 |
| Subject: | Re: Negative string offset support | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-13628@lists.php.net to get a copy of this message | ||
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 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