On Mon, Jun 11, 2012 at 12:13 PM, Stas Malyshev <smalys...@sugarcrm.com>wrote:

> Hi!
>
> >> Can be:
> >> $var = 'abc';
> >> echo $var[-1];
> >
> > This seems simple enough for a hard-coded -1, but...
> >
> > Would $var[-2] be strlen($var) - 2 and so on?
>
> The main question is what happens with "foo"[-4] or ['x'][-2].
>

The same thing that currently happens for:

    $f='foo'; echo $f[strlen($f)-4];
    $a=['x']; $a[count($a)-2];

which appears to be notices:

Notice: Uninitialized string offset: -1 in - on line 2
> Notice: Undefined offset: -1 in - on line 3


> And then one would expect some rather complex logic to compute -N for
> > $var[-N]
>
> I don't see much of complex logic here, but $a[2] = 'a' would create a
> new array element if it does not exist, while $a[-2] can't. Not a big
> issue, but somewhat inconsistent I guess.
>

Negative indices are currently valid for arrays. If anyone makes use of
negative indices, this could break their script. Personally, I'd rather
have Marc Easen's behavior.

Reply via email to