What's this kind of syntax in PHP?

$test= <<<EOF



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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

(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
  Hello, $a!
echo $string;

It will give you this output:

Hello, World!

And this HTML source:

  Hello, World!

Solution 2

That would be Heredoc

Solution 3


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.

Solution 4

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

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply