4
$test= <<<EOF

....

EOF;

I have never see it before. What's it used for?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user198729
  • 61,774
  • 108
  • 250
  • 348

4 Answers4

15

This is called HEREDOC syntax, which is a way to define strings, on multiple lines, with variable interpolation.


Quoting the manual page:

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.

(There is more to read, that I didn't copy-paste from the manual page)


And, as a very quick and simple example:

$a = 'World';
$string = <<<MARKER
<p>
  Hello, $a!
</p>
MARKER;
echo $string;

It will give you this output:

Hello, World!

And this HTML source:

<p>
  Hello, World!
</p>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • What do you mean by *"nested case"* ? – Pascal MARTIN Feb 25 '10 at 12:14
  • It's just a way to define a string without having to escape any special characters. It doesn't _do_ anything, really, so the question seems a bit odd. – Alan Plum Feb 25 '10 at 12:19
  • FYI The NOWDOC is the new HEREDOC: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc – Phill Pafford Feb 25 '10 at 13:40
  • NOWDOC, when first created, was different : it was using simple-quotes only, and there was no variable interpolation ;;; NOWDOC with double-quotes (and variables interpolation) has been added later, for consistency -- and HEREDOC still exists. – Pascal MARTIN Feb 25 '10 at 17:05
0

That would be Heredoc

Stoosh
  • 2,408
  • 2
  • 18
  • 24
0

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Blockquote 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.

The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

Orentet
  • 2,353
  • 1
  • 17
  • 28
0

Indeed, this is the Heredoc syntax.

Just in case you are wondering what interest it can have vs the regular string delimiters:

// These strings contain the same thing '"'"
$s1 = '\'"\'"';
$s2 = "'\"'\"";
$s3 = <<<EOS
'"'"
EOS

No more quote escaping.
A typical use case for me is when I need to store in a string some HTML code I have copy/pasted.

Benoît Vidis
  • 3,908
  • 2
  • 23
  • 24