10

Possible Duplicate:
php string escaping like python’s “”“ ”“”?

The triple-quotes in python escapes all quotes and newlines contained within. For example,

""" this
is all


just one string. I can even tell you "I like pi".
Notice that the single quotes are escaped - they don't end the string; and the newlines become part of the string
"""

Does anybody know if PHP has an equivalent to python's

""" <form><h1>PUT HTML HERE</h1> </form> """
enter code here

EDIT: For those looking at this question in the future, I have answered it, here is an example:

$heading = "Heading Gettizburgz"; print <<< END <p><h1>$heading</h1> "in quotes" 'in single' Four score and seven years ago<br/> our fathers set onto this continent<br/> (and so on ...)<br/> </p> END;

prints: Heading Gettizburgz "in quotes" 'in single' Four score and seven years ago our fathers set onto this continent (and so on ...)

Note one important thing, you must make sure that the very last END is to the far left (fist column) of your code without ANY spaces before it.

source: http://alvinalexander.com/blog/post/php/php-here-document-heredoc-syntax-examples

4
  • 1
    @RobertHarvey, I think you didn't get the question because Markup swallowed all the newlines. Not the fault of the questioner at all. Dec 17, 2012 at 22:53
  • @MarkRansom: Is this a "General Reference" question, or is it obscure enough that it might benefit others? The answers posted so far seem fairly mundane. Dec 17, 2012 at 22:54
  • @RobertHarvey: I have searched for a similar question in the past (when I was learning PHP). I don't think this is a bad question. I think it has a place here on SO Dec 17, 2012 at 22:56
  • Sorry guys, it looks like some of you saw what I just noticed -- in python you can have triple quotes and then put any HTML that you'd like within the three quotes as long as you close the string with """. I am looking for a way to do that within PHP because as it is I am always doing things like: echo "<h1>" . $myVar . "</h1>" ; whereas in Python it is something like: myVar2 = """ <h1> myVar </h1> """ print myVar2 Hope that helps, I'll try and be clearer next time. :) Dec 17, 2012 at 23:13

1 Answer 1

11

You can use heredocs or nowdocs (see below heredocs).

Heredoc

$bar = <<<EOT
bar
EOT;

Nowdoc

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
2
  • 2
    Thanks Michael. You would be suprised at how much time I have wasted escaping stuff in my functions.... thank you so much! Dec 17, 2012 at 23:17
  • No problem. Glad that solved your problem :) Dec 17, 2012 at 23:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.