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

With a regular 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 the heredoc .. string?

 $str = <<<EOD Hello world EOD; $str .= <<<EOD bla bla bla"; bla bla bla..."; EOD; 
+7
string php string-concatenation heredoc
source share
3 answers

Of course. Why don't you let it?

Heredocs evaluates the string, so this is perfectly acceptable.

+12
source share

Yes, you can. Heredoc is part of the expressions, so you can do this:

 $s = 'abc' . <<<EOD def EOD . 'ghi'; 

Be careful with the end-of-data marker: it must be the only one in the row.

+8
source share

Yes, you can.

Heredoc is equivalent to double quotes without double quotes.

+6
source share

All Articles