14

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;
codaddict
  • 445,704
  • 82
  • 492
  • 529
pnichols
  • 2,198
  • 4
  • 24
  • 29
  • 4
    -1 really, you can test that all by yourself. If you don't have a local PHP install google for `execute PHP code online`... – NikiC Oct 22 '10 at 19:14
  • 1
    You really should have been able to figured out whether this runs on your own rather than asking SO. The code works exactly as you posted it. Experimentation is key to understanding. – user229044 Oct 22 '10 at 19:48
  • 2
    Agreed, I could've tested this... But I couldn't find this info with a quick Google search, and I'm probably not the first and/or last one to have this exact question. Now this info will be readily available thanks to the answers below. – pnichols Oct 22 '10 at 20:53
  • You are wrong, others should test test it as well, not google for it. BTW, note that heredoc is useless. You will never need concatenate heredocs, nor herecoc itself. Learn PHP better. Large amount of text should be typed as is, outside of PHP tags. – Your Common Sense Oct 23 '10 at 06:27
  • 13
    I'm with the asker. It's a simple yes/no question that's quicker to Google than to experiment with, and others can learn from it. We don't all learn the same way, so folks shouldn't expect what works for them to work for everyone else. – Marcus Jan 07 '11 at 16:57
  • I'd like to know a simple answer instead of testing it myself... saved myself a minute of my precious life. – netrox May 17 '12 at 18:48
  • 2
    Good, small question. Saved me 1/2 mins of testing which is OK. – Alan Sep 01 '14 at 10:23
  • I confirm with @Marcus !!! And what you should keep in mind, just because of such questions google can provide easy answere instead of running some tests thousends of times. even 12 years later this answere is helpfull. and only thing what costs me time is reading and answering unnecessary comments ^^ – Dwza Mar 08 '22 at 14:17

3 Answers3

20

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

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

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
13

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

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

Be careful with the end-of-data marker though: it should be the only thing on a line.

mojuba
  • 11,842
  • 9
  • 51
  • 72
10

Yes you can.

A heredoc is equivalent to double quoted string without the double quotes.

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • Ouch, so a heredoc in PHP is NOT a string literal. Which it normally is, in other languages. – Henk Poley Aug 30 '19 at 07:44
  • 2
    @Henk Poley PHP has those too, it's called [nowdoc](https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc). – Adrian Wiik Sep 08 '20 at 13:16