Didn't I understand what heredoc should do?

I am very new to PHP, so I know that I am missing something obvious here - I thought the heredoc function should preserve formatting, line breaks, etc. But whenever I test it while it parses, there is no formatting. I tried many different scripts, including copies and past versions from sources such as PHP.net and W3schools, so I know that there is nothing wrong with scripts. I can not answer this question - perhaps because it is too obvious ??? (BTW, testing with MAMP). Thank you

+3
source share
4 answers

HEREDOC is not a function, it is a method for specifying line separators that allows you to refuse quotes in heredoc (I like it because good text editors will syntactically highlight their contents based on the name heredoc).

HEREDOCs retain all spaces, newlines, etc. I think you are probably looking at the results in a browser. Browsers usually trim all spaces, newlines, spaces and tabs, down to one place. Try looking at it from the command line, text message, or text file.

+4
source

It will create a string identical to the one you set.

However, browsers display multiple spaces, compressed to a single space character. This is by design.

, pre ( , ) white-space: pre CSS.

<div style="white-space: pre">
<?php echo <<<A
some text
    preserved
        just
    how
 you want it.
A; ?>
</div>

jsFiddle.

+3

Since you said "testing with MAMP", I assume that you are using your web browser to display the content. This is a slightly wrong approach, because the webbrowser itself removes all the extra spaces. Look at the source of the page displayed in your browser and you will see the “real” content.

+3
source

He does, but only for what he infers.

If it outputs HTML, then this HTML code will be interpreted by the browser using the usual rules for handling spaces.

0
source

All Articles