Click here to get back home

heredoc and array problems

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
heredoc and array problems Jeff 07-17-2008
Posted by Jeff on July 17, 2008, 2:05 pm
Please log in for more thread options
I have this:

$content = <<<TD

$D['section']

TD;

that fails silently and stops php even though errors are turned on

This works:

$content = <<<TD



TD;

Now, I just stumbled on that "fix" and it took me a loooong time to
figure out where the problems laid.

What's going on here?

This isn't much help:

http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Jeff

Posted by macca on July 17, 2008, 4:16 pm
Please log in for more thread options
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 ing";

would output "I am Talking";


Hope this helps. :-)

Posted by Jeff on July 17, 2008, 4:44 pm
Please log in for more thread options
macca wrote:
> 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.

>
> 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!

>
> 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
>
>
>
> 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. :-)

Similar ThreadsPosted
Array Problems November 13, 2004, 1:07 pm
problems with sorting an array December 3, 2004, 11:15 am
problems sorting an array March 30, 2005, 2:06 pm
crlf causes problems in array functions November 3, 2006, 6:57 pm
Problems with matching on last element in array April 9, 2008, 3:22 pm
question about HEREDOC March 15, 2005, 11:46 am
using heredoc for SQL statements January 16, 2006, 7:27 pm
Getting a function result while in heredoc December 19, 2006, 11:15 am
inclosed variable heredoc December 19, 2006, 5:06 pm
HEREDOC help, going quietly nuts October 20, 2007, 11:44 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap