Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to concatenate heredoc string in PHP..?

With normal PHP string you can do this:

$str = "Hello ";
$str .= "world";
$str .= "bla bla bla";
$str .= "bla bla bla...";

But can you do the same with heredoc string..?

$str = <<<EOD
Hello 
world
EOD;

$str .= <<<EOD
bla bla bla";
bla bla bla...";
EOD;
like image 807
pnichols Avatar asked Oct 22 '10 19:10

pnichols


People also ask

Can you concatenate strings in PHP?

There are two string operators. The first is the concatenation operator ('. '), which returns the concatenation of its right and left arguments.

What is heredoc string in PHP?

The heredoc syntax is a way to declare a string variable. The heredoc syntax takes at least three lines of your code and uses the special character <<< at the beginning.

What is a heredoc string?

In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file.

What is the difference between heredoc and Nowdoc in PHP?

Heredocs and nowdocs are the easiest and cleanest ways in php to use and format multiline strings. The only difference between a heredoc and a nowdoc is that a heredoc performs string interpolation, turning your variables into the string they represent, while a nowdoc does not.


1 Answers

Of course. Why wouldn't you be able to?

Heredocs evaluate to a string, so this is perfectly acceptable.

like image 71
Jacob Relkin Avatar answered Oct 03 '22 23:10

Jacob Relkin