4

By the docs:

Heredoc text behaves just like a double-quoted string, without the double quotes.

this code

<?php
echo "dc:<"."


".">";
echo "hd:<".<<<EOD


EOD
.">";

should output:

dc:<


>hd:<


>

but instead (on PHP V5.6.13 for Windows) it outputs:

dc:<


>hd:<
>

What's up?

ChrisJJ
  • 2,191
  • 1
  • 25
  • 38

1 Answers1

5

I experimented with this and I think that I can make an educated guess.

In the double quoted string section you have 3 newlines: one from line 2 to line 3, one from line 3 to line 4, and one from line 4 to line 5.

In the heredoc section you only have one newline. I think that the heredoc section starts on the line AFTER the first EOD token and ends on the line BEFORE the last EOD token. So in your heredoc you only have one newline from line 7 to 8 and you can see that in your output (the newline between the < and the > at the end). If you add more newlines to the heredoc section then you will see them in your output.

Hopefully this will help illustrate:

<?php
echo "dc:<"."          <-- first newline
                       <-- second newline
                       <-- third newline
".">";
echo "hd:<".<<<EOD     <-- heredoc starts after this part
                       <-- only one newline here
                       <-- heredoc ends here so this newline doesn't count
EOD
.">";


In response to your comments:

"In the heredoc section you only have one newline." I have three

Yes, but as I have said, I think that the first and last newlines are ignored.

"I think that the heredoc section starts on the line AFTER the first EOD token and ends on the line BEFORE the last EOD token" No. See the doc quote I posted.

Look at this text from the doc that you linked to (emphasis mine):

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.

Notice that it says that the string itself follows the newline. I believe that this proves my assertion that first newline is omitted as it is part of the heredoc construct. Admittedly this only explains the first newline. But I believe that the last newline is ignored for the same reason.

"If you add more newlines to the heredoc section then you will see them in your output." Yup, but still my output has the wrong quantity of newlines.

That quote being: "Heredoc text behaves just like a double-quoted string, without the double quotes."

I think that if your double quoted string section was structured like this...

<?php
echo 'dc:<'.
"
"
.'>';
echo "hd:<".<<<EOD


EOD
.">";

Then it would work like you expect. In other words, the heredoc works the same as double quoted string without the double quotes. Try it.

Cave Johnson
  • 6,499
  • 5
  • 38
  • 57
  • "In the heredoc section you only have one newline." I have three. "I think that the heredoc section starts on the line AFTER the first EOD token and ends on the line BEFORE the last EOD token" No. See the doc quote I posted. – ChrisJJ Nov 20 '16 at 01:54
  • " If you add more newlines to the heredoc section then you will see them in your output." Yup, but still my output has the wrong quantity of newlines. – ChrisJJ Nov 20 '16 at 01:55
  • That quote being: "Heredoc text behaves just like a double-quoted string, without the double quotes." – ChrisJJ Nov 20 '16 at 01:55
  • Hi @ChrisJJ, I added responses to your comments in my answer. I hope they are helpful. – Cave Johnson Nov 23 '16 at 19:44
  • I think your 'double quoted string section structured like this' with blank lines moved to outside the double-quoted is a good mnemonic for working around the failure of heredoc newline(s). But I think it does not make "Heredoc text behaves just like a double-quoted string, without the double quotes." true. The best we can say is "Heredoc text behaves just like a double-quoted string from which any single leading and any single trailing blank line have been removed, without the double quotes". An inelegant description of an inelegant fact. – ChrisJJ Dec 04 '16 at 14:22
  • Thanks for that summary. It clarifies that a) The documentation fails to accord internally b) the documentation and behaviours fails to accord with each other. I think your "heredoc ends here so this newline doesn't count" would be better worded "this newline doesn't count so heredoc ends here". I can't see any good reason for this final blank line to be ignored, so I conclude this is not working as originally intended. Clearly a fix is prevented by the need for compatibility with existing workedaround code. – ChrisJJ Dec 04 '16 at 14:22