[thelist] PHP - need clarification of quotes in array names.

Johnny Jørgensen aggie at aggie.dk
Wed Feb 6 19:25:00 CST 2002


--- Afonso Fernandez Nogueira <afonso at writeme.com> wrote:
> > How then do I echo $foo['bar'] as part of a string? if I have:
> > $foo['bar'] = "hello";
> > echo "$foo['bar'] world";
> > This gives an error.
> > If I have
> > $foo['bar'] = "hello";
> > echo "$foo[bar] world";
> > This works, but is it wrong?

well, arrays are always causing a mess in strings - especially as they
grow more complex - $foo[$bar[$i]['name']]['id'] might seem (somewhat)
logical to me, but the parser has a hard time - not to mention the added
headache of the string. The answer lies in { } - try

$foo['bar'] = "hello";
echo "well, {$foo['bar']} world";

- the $foo[bar] construction works, which is odd, since bar should
represent some undefined constant - it seems to me PHP falls back on
variables in the symbol table, if there's no constant?

--
Johnny Jørgensen <aggie at aggie.dk>

"Eagles may soar, but weasels don't get sucked into jet engines"




More information about the thelist mailing list