views:

213

answers:

4

For example:

$sql = <<<MySQL_QUERY

Thank you!

+9  A: 

It is the start of a string that uses the HEREDOC syntax.

A third way to delimit strings is the heredoc syntax: <<<.

After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

Pekka
Cool, I didn't know that one... I read the link you sent and honestly, I understand why I could live without that one so far! ;) What would be the advantage of using that for a string?
Gabriel
@Gabriel it allows you to use both kinds of quotations inside the string without breaking it: `<<<END Hello "$name"! How is '$spouse' today? END` One huge pain in the ass is, however, that the `END` marker must not be indented, so Heredoc content usually breaks your code's indentation. It's indeed not really one of PHP's most important features :)
Pekka
@YiJiang that was actually because of the german localization! Damn geolocation, always gives me the wrong link even though english is my browser language. Corrected, cheers :)
Pekka
I can be very useful for the right tasks, though.
Hugo Estrada
@Pekka: Thank you for the explanation! Perfectly clear! :)
Gabriel
@Gabriel you're welcome. @Hugo does have a point, though, there are situations where they *do* make sense and help make the code more readable. But the missing indenting capability takes a lot away at least in my practice
Pekka
+3  A: 

Its PHP's heredoc

Example:

$sql = <<<MySQL_QUERY
SELECT * 
FROM TAB 
WHERE A = 1 AND B = 2 
MySQL_QUERY;           
codaddict
+1  A: 

Its the heredoc syntax.

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
Russell Dias
+2  A: 

That's heredoc syntax. You start a heredoc string by putting <<< plus a token of your choice, and terminate it by putting only the token (and nothing else!) on a new line. As a convenience, there is one exception: you are allowed to add a single semicolon after the end delimiter.

Example:

echo <<<HEREDOC
This is a heredoc string.

Newlines and everything else is preserved.
HEREDOC;
tdammers