0

Possible Duplicate:
Have I misunderstood what heredoc should do?

I read that Here document preserves the line breaks and other whitespace (including indentation) in the text. But when I run the following script,everything gets printed on the same line. Why is it so ?

<?php 
$str = <<<HDC
 This is a sample text
 Some more sample text
 Even more sample text
HDC;
echo $str;
Community
  • 1
  • 1
saplingPro
  • 20,769
  • 53
  • 137
  • 195

2 Answers2

1

The output actually does contain the line breaks. However, HTML (by default) ignores line breaks.

If you want the HTML to render the line breaks, wrap it in a pre:

<pre>
<?php 
$str = <<<HDC
 This is a sample text
 Some more sample text
 Even more sample text
HDC;
echo $str;
?>
</pre>
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
0

Or if you cannot use <pre> because <pre> always prints in fixed width, manually insert the <br> tags.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
Amit
  • 1,836
  • 15
  • 24