|
Posted by Jeff on July 17, 2008, 4:44 pm
Please log in for more thread options macca wrote:
show/hide quoted text
> 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.
OK, I understand that now. Perls heredocs don't do that.
show/hide quoted text
>
> 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!";
I see that if I do this:
$D[section] = 'some_var';
I get this:
Use of undefined constant section - assumed 'section' in..
So the implied interpolation works only (without notice) inside strings.
Which is a shame as it looks like perl to me without the quotes!
show/hide quoted text
>
> The same is true for HEREDOC.
>
> Thus:
>
> $D['section'] = "hello";
>
> echo $content = <<<TD
>
> $D[section]
>
> TD;
>
> would print "hello".
Thanks, I've got it under control now!
Jeff
show/hide quoted text
>
>
>
> 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 ing";
>
> would output "I am Talking";
>
>
> Hope this helps. :-)
|
>
> 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.