Re: Array/string offset [] vs. {} syntax

From: Date: Sat, 26 Aug 2000 18:00:40 +0000
Subject: Re: Array/string offset [] vs. {} syntax
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-30777@lists.php.net to get a copy of this message
At 10:56 AM 8/26/00 -0700, Lars Torben Wilson wrote:
I just noticed this because of Andre's bug report #6180: since string offset support using {} has been added, it appears that [] and {} may be used interchangeably for array indexing and string offsets, except for inside strings, where the {} doesn't work as I might have expected: $str = 'This is a test'; echo "Second char of \$str: ${str[2]}\n"; echo "Second char of \$str: {$str{2}}\n"; echo "Second char of \$str: $str{2}\n"; echo "Third char of \$str: " . $str[3] . "\n"; echo "Fifth char of \$str: " . $str{5} . "\n"; Gives output: Second char of $str: i Second char of $str: i Second char of $str: This is a test{2} Third char of $str: s Fifth char of $str: i Can we get a rundown at some point about what the full implications are, or if the support is complete yet? With the latest cvs, I get: $a = "Z"; $a{0} = "f"; print_r($a); // prints 'f' echo "\n"; $a = "Z"; $a{1} = "f"; print_r($a); // prints 'Z' echo "\n"; $a = "ZZ"; $a{1} = "f"; print_r($a); // prints 'Zf' echo "\n"; Is this what is expected, and should it be going in the manual?
To be honest {} should only work for string offsets but we haven't gotten around to changing this yet. I made the change in Zend necessary to support this but never got around to fully implementing it. I will try and address this in 4.0.3. DON'T use {} to index arrays. It will not work in future versions. As for {} support within encapsulated strings. It will work but as I said I just put in the infra-structure but haven't implemented it yet. I thought no one had noticed :) Andi --- Andi Gutmans <andi@zend.com> http://www.zend.com/

« previous php.dev (#30777) next »