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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

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

Post 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
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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).
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post 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
Post Reply