Re: heredoc and array problems



You need to look up Interpolation.

Heredoc follows similar interpolation capabilities as echoing with
double quotes.

When you echo an array variable such as $D['section'] in SINGLE QUOTES
you need to concatenate it as single quotes means a string literal
like so:

echo 'I have to escape '.$D['section'].' to output correctly';

When you echo an array variable such as $D['section'] in DOUBLE QUOTES
you do not need to concatenate the array variable (although you can if
you wish) as double quoted strings are INTERPOLATED (or converted)
prior to being written to output.

However, to include an array variable inside an interpolated (double
quoted) string you would leave out the single quotes (which is
perfectly legal syntax) like so:

echo "I don't have to escape $D[section] to output correctly but don't
need the single quotes either!";

The same is true for HEREDOC.

Thus:

$D['section'] = "hello";

echo $content = <<<TD

$D[section]

TD;

would print "hello".



Using curly braces {} is more for outputting variables where the
continuation of the string would make it difficult to interpolate it
properly such as part of a word. Basicly curly braces say: {this is a
variable}

e.g.

$var = 'Talk';

echo "I am $varing";

would not work because $varing is not a defined variable.

echo "I am {$var}ing";

would output "I am Talking";


Hope this helps. :-)
.



Relevant Pages

  • Re: heredoc and array problems
    ... When you echo an array variable such as $Din SINGLE QUOTES ... you need to concatenate it as single quotes means a string literal ... So the implied interpolation works only inside strings. ...
    (comp.lang.php)
  • Re: AIX FTP question.
    ... >> echo binary ... in the script will turn it back on! ... Single quotes protect every character except another single quote ...
    (comp.unix.shell)
  • Re: Disabling shell expansion
    ... the problematic log entry is not IN a log ... printf produces the same results as echo. ... With Double quotes it works fine.. ... >if I use single quotes then other single quotes in the message end the ...
    (comp.unix.shell)
  • Re: FAQ 8.28 How can I call backticks without shell processing?
    ... )> I think you meant to have "echo" not 'echo', ... I missed the fact that it was called on the command-line, ... double quotes in Perl. ... quotes for "-|" but single quotes for 'echo'. ...
    (comp.lang.perl.misc)
  • Re: FAQ 8.28 How can I call backticks without shell processing?
    ... )> I think you meant to have "echo" not 'echo', ... double quotes in Perl. ... quotes for "-|" but single quotes for 'echo'. ... But when typing commands ...
    (comp.lang.perl.misc)