Page 1 of 1

[SOLVED]heredoc - use restricted only to echo...

Posted: Tue Jun 08, 2004 11:42 am
by batfastad
Very quick question.

I've just started PHP and I'm working steadily on a PHP-based site, using whatever tricks I can learn.

The other day someone informed me about heredoc syntax - wow!!
That's pretty useful rather than having to escape quotes etc.

My question is, can heredoc syntax only be used for the echo command like below...?

Code: Select all

echo <<<HTML
.....
HTML;
Or can you also use it for the setting of variables for example?

Code: Select all

$foo = <<<HTML
all sorts of stuff here
HTML;
Thanks

Posted: Tue Jun 08, 2004 11:53 am
by launchcode
Don't take this the wrong way - but did you ever think to just try it and see? It would have taken less time than it took to post this forum message :)

And the answer is - yes you can use it in the manner you wish (i.e. variable assignment).

Posted: Tue Jun 08, 2004 12:16 pm
by batfastad
I did try it and got an error.

But couldn't tell whether it was my code or whether it doesn't work.
Turns out it's my code, and there it is!
A misplaced ;

Thanks

Posted: Tue Jun 08, 2004 5:22 pm
by pickle
If you're new to heredocs remember, the closing heredoc has to be the ONLY thing on it's line. No characters before or after the declaration, including whitespace (that problem seems to creep into my stuff every once in a while).

Posted: Wed Jun 09, 2004 4:39 am
by batfastad
Yep, got that sussed!!
I'm finding learning PHP a good experience. Things are starting to make sense, and it's easy to write powerful scripts.

Thanks for your help