Hi internals,

PHP has a couple of legacy string interpolation syntaxes, the most
egregious being "$array[foobar]". The issue with this syntax is that the
array key is not quoted, is required to be not quoted and is silently
accepted.

We've been fighting back against unquoted strings for a long time,
culminating with $array[foobar] in normal code becoming an Error exception
in PHP 8, as the string fallback for unknown constants has been removed.

In this context, it is particularly weird that "$array[foobar]" continues
to be silently allowed, and the more expected form "$array['foobar']"
yields an inscrutable error message:

> Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING)

I think there's two ways to address this. One is to deprecate and
eventually remove the non-wrapped array interpolation syntax entirely,
requiring people to use the generic "{$array['foobar']}" syntax instead.
For the sake of consistency, I think this would also include deprecating
the "$array[0]" variant.

The other is to add support for "$array['foobar']" and
"$array[some(complex(expression()))]" in general, and only deprecate the
"$array[foobar]" syntax.

What do you think about this? I think my personal preference would be
towards phasing out the syntax entirely: If we're going to make people
migrate, we can just as well migrate them towards our generic and preferred
interpolation syntax.

Regards,
Nikita

Reply via email to