4

I dimly recall, from my first readings of the PHP docs (more than 10 years ago) that the array-like syntax to access characters in arrays ($string[0]) posed some ambiguity or undefined behaviour.

The O'Reilly PHP Pocket Reference (2nd ed) states:

To solve an ambiguity problem between strings and arrays, a new syntax has been introduced to dereference individual characters from strings:

$string{2}

This syntax is equivalent to $string[2], and is preferable.

I understand that $string[2] might be confusing, but I'm not sure how it could be ambiguous?

Furthermore: I wonder how the new syntax $string{2} removes the ambiguity/confusion, considering that the curly braces (apparently) also work for "real" arrays.

Community
  • 1
  • 1
leonbloy
  • 65,169
  • 19
  • 130
  • 176
  • Im not sure if i understand it correctly but i don't see a problem here, since $string{2} (or $string[2]) is used to either, from the perspective of this being a string, select the 2nd character or from the perspective of an array, retreive the 2nd element, which is basically the same thing for the interpreter since a string is just an array too for the interpreter. – Garuda May 06 '12 at 01:42
  • latest php version gets weird `$str = 'hi'; echo $str[0][0][0][0][0][0][0];//outputs h`, just recursively does substr for the first char – goat May 06 '12 at 03:45

3 Answers3

6

The only ambiguity is that if you're expecting an array, but actually have a string, $var[0] will give you the first byte of the string instead of the first array element. This may lead to a lot of head scratching and wondering why PHP is only giving you the first character instead of the whole array element. This is even more true for non-numeric indexes like $var['foo'], which actually works if $var is a string (yeah, please don't ask). I.e. it may make debugging slightly more difficult if your program is wrong in the first place.

There's no ambiguity for correct programs, since a variable cannot be a string and an array at the same time.

deceze
  • 471,072
  • 76
  • 664
  • 811
  • 1
    +1 and a rant: This is a typical PHP "solution" that... solves nothing. With `[]` by mistake on a string you get an unexpected result while with `{}` by mistake on an array you also get an unexpected result since you were always expecting one character only. Of course that's not true because as the OP says `{}` works "normally" on arrays too, so now `{}` has the same problem that `[]` had that led to `{}` being introduced. Perhaps a future attempt will manage to get this right before we run out of punctuaction. Ugh. – Jon May 06 '12 at 01:56
  • `$var['foo']` works because PHP knows it's not an array, 'foo' is not a valid key for this non-array, so its coerced into a 0 and becomes `$var[0]` – Marc B May 06 '12 at 03:20
  • @Marc Yes, there's an explanation, but that doesn't really make it better. :) The behavior in 5.4 of at least triggering a notice makes a lot more sense. – deceze May 06 '12 at 03:40
2

Many problems caused by the ambiguity between string offsets and array offsets have been removed with the changes in 5.4, which is after the publish date of your reference. http://php.net/manual/en/migration54.incompatible.php

For this reason, I'd reccomend [] for string offsets in new code.

Cory Carson
  • 266
  • 1
  • 6
1

Well, I have tested some variables with this code:

<pre><?php 

dumpling(array("php"));
dumpling(array());
dumpling(0);
dumpling(1);
dumpling(TRUE);
dumpling(FALSE);
dumpling(NULL);
dumpling("php");

function dumpling($var){
    var_dump($var[0]);
    var_dump($var{0});
}

?>

and there didn't seem to be any difference between those two.

The output was :

string(3) "php"
string(3) "php"
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
string(1) "p"
string(1) "p"
Taha Paksu
  • 14,293
  • 1
  • 39
  • 67