Yep, an unfortunate "feature" of PHP is that an unquoted string used as an array key will be considered a constant, and if that constant has not yet been [man]define/mand, then it will be "self-defined" as having a value equivalent to its name, and a notice-level warning will be raised. However, to confuse things further, an associative array being interpolated within a double-quoted or [man]heredoc[/man] string must not have the key quoted, unless you use the "complex" variable notation (curly braces), in which case it should be quoted again as if it were not within a quoted string. :rolleyes:
So, the following are all valid:
$foo['bar'] = 'test';
echo "This is a $foo[bar].";
echo "This is a {$foo['bar']}.";
echo "This is a " . $foo['bar'] . ".";
// heredoc method:
echo <<<END
This is a $foo[bar].
This is another {$foo["bar"]}.
END;
printf("This is a %s.", $foo['bar']);
This will result in a parse error:
echo "This is a $foo['bar'].";
This will throw the "undefined constant" notice:
echo "This is a " . $foo[bar] . ".";