Page 1 of 1
Neater way to store strings
Posted: Wed Dec 12, 2007 3:36 am
by impulse()
I stumbled across a PHP tips article a few days back which showed a neat way to store strings and it was something similar to:
Code: Select all
$var <<
Put whatever
you want like
this
and it just works;
But that's not the correct way to do it because it throws up parse errors. What is this called or where would I find an example?
Posted: Wed Dec 12, 2007 3:48 am
by Christopher
It is called "heredoc syntax" and can be found in the section of the manual about string types:
http://www.php.net/manual/en/language.types.string.php
Posted: Wed Dec 12, 2007 3:49 am
by nathanr
Code: Select all
$var = <<<DELIMITERNAME
Put whatever
you want like
this
and it just works
DELIMITERNAME;
heredoc and string types tokens

Posted: Thu Dec 13, 2007 4:04 pm
by impulse()
Thank you.
Posted: Thu Dec 13, 2007 4:45 pm
by nickvd
One quick note that caused me hours of frustration... the ending delimiter CANNOT BE INDENTED... It took me hours of searching before I accidentally figured it out

Posted: Thu Dec 13, 2007 9:23 pm
by Benjamin
nickvd wrote:One quick note that caused me hours of frustration... the ending delimiter CANNOT BE INDENTED... It took me hours of searching before I accidentally figured it out

To me that is a reason not to use it in and of itself. Spose there is some reason why it has be like that though. Anyway, if you have a string that long, there is probably a better way to store it.
Posted: Thu Dec 13, 2007 9:45 pm
by John Cartwright
astions wrote:nickvd wrote:One quick note that caused me hours of frustration... the ending delimiter CANNOT BE INDENTED... It took me hours of searching before I accidentally figured it out

To me that is a reason not to use it in and of itself. Spose there is some reason why it has be like that though.
Anyway, if you have a string that long, there is probably a better way to store it.
Indeed, I have never even used heredoc before for that exact reason.