Triple quoted strings

jeff.ecmascript at tanasity.com jeff.ecmascript at tanasity.com
Wed Mar 5 15:44:02 PST 2008


If I understand the motivation for triple quoted strings, it's to allow 
multiline strings with inverted commas and quotes within them, and to 
allow quotes and inverted commas within those substrings, all without 
requiring a continuation marker.

PHP has a structure that's used for this sort of thing, which works 
quite well and is used when you have a large block of text tobe used as 
a string. As an example of when this is used, commonly, emails that are 
constructed in code use this technique.

The heredoc syntax is:

----
$var = <<<EndOfStringMarker
This text is part of the string. It can include " and '. It can include 
references to one or more $variable, each of which is replaced with the 
value of the variable. This string includes the
line breaks without needing backslash syntax like backslash n to 
introduce a newline. Consequently the previous sentence includes a break 
after the fourth word.
To end this string you must have whatever end of string marker you have 
chosen on a line by itself. Thusly...
EndOfStringMarker;
----

PHP documentation on the feature is at 
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

IMO this is a useful but fairly infrequently used language facility. 
Constructing large blocks of text without this facility would be harder, 
but losing the facility would not be a major blow.

Jeff Veit







More information about the Es4-discuss mailing list