Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP equivalent to python's triple-quotes - How to print bulk / lots of HTML within PHP without escaping [duplicate]

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

like image 919
Alex Spencer Avatar asked Dec 17 '12 22:12

Alex Spencer


People also ask

In what instances would you use triple quotes instead of single or when defining a string in Python?

Spanning strings over multiple lines can be done using python's triple quotes. It can also be used for long comments in code. Special characters like TABs, verbatim or NEWLINEs can also be used within the triple quotes.

How do you print quotation marks in PHP?

A double-quoted string will output \' with either a single or double backslash used with the apostrophe. To output the \" sequence, you must use three backslashes. First \\ to render the backslash itself, and then \" to render the double quote.

Is it better to use single or double quotes Python?

Use single-quotes for string literals, e.g. 'my-identifier' , but use double-quotes for strings that are likely to contain single-quote characters as part of the string itself (such as error messages, or any strings containing natural language), e.g. "You've got an error!" .

What will happen if we represent string with triple quotes in Python?

String literals inside triple quotes, """ or ''', can span multiple lines of text. Python strings are "immutable" which means they cannot be changed after they are created (Java strings also use this immutable style).


1 Answers

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;
like image 115
Michael Mior Avatar answered Oct 08 '22 03:10

Michael Mior