Neater way to store strings

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Neater way to store strings

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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
(#10850)
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

Code: Select all

$var = <<<DELIMITERNAME
Put whatever
you want like
this
and it just works
DELIMITERNAME;
heredoc and string types tokens

:)
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Thank you.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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 ;)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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