Re: heredoc and array problems
- From: macca <ptmcnally@xxxxxxxxxxxxxx>
- Date: Thu, 17 Jul 2008 13:16:50 -0700 (PDT)
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. :-)
.
- Follow-Ups:
- Re: heredoc and array problems
- From: Jeff
- Re: heredoc and array problems
- References:
- heredoc and array problems
- From: Jeff
- heredoc and array problems
- Prev by Date: Re: Aggregate or extending a class alternatives ?
- Next by Date: Re: heredoc and array problems
- Previous by thread: Re: heredoc and array problems
- Next by thread: Re: heredoc and array problems
- Index(es):
Relevant Pages
|