Re: deprecation status of $str{42} versus $str[42]
| From: | Edward Z. Yang | Date: | Tue, 17 Jun 2008 11:35:05 +0000 |
| Subject: | Re: deprecation status of $str{42} versus $str[42] | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-38321@lists.php.net to get a copy of this message | ||
Stefan Walk wrote:
> You're not learning from the mistakes of other languages (ruby in this case,
> which removed Enumerable from String in 1.9) ... "foreach" makes no sense for
> strings, because it's unclear what you want (with unicode terminology here,
> as this is for php6):
> "for each byte" "for each codeunit" "for each codepoint", or
> "for each line",
> or ...
You bring up a good point. However, as a counterpoint, Python does allow
strings to be used as arrays:
s = 'asdf'
for c in s:
print c
In my opinion, it only makes sense for foreach to emulate the code
snippet I posted above: for binary strings, that means byte-by-byte, and
for Unicode strings, that means codepoint by codepoint.
But as I said, it would be "neat." This is by no means an essential
feature, and I probably wouldn't be able to use it anyway for BC concerns.
> if you want to use foreach in your example, just do
> foreach (str_split($str) as $value) { ...
I would never do that, because it requires allocating an entire another
PHP array, more than doubling memory usage, just to iterate across a
string. If I'm doing this sort of heavy string processing, I probably
need some semblance of performance.
--
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <http://htmlpurifier.org> Anti-XSS Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]