\n vs. PHP_EOL vs.
?

后端 未结 6 1872
粉色の甜心
粉色の甜心 2020-12-04 18:04

In most cases, as for one interactive website, when we output multiple lines of contents to web client browser, in my opinion,
is much more prefer

6条回答
  •  一个人的身影
    2020-12-04 18:47



    is only to be used when dividing up text. You may see this used for spacing out elements within an html document, but that is not its purpose, as layout spacing should be achieved with CSS such as margin. Here's a decent use case:

    This
    is
    some
    text.

    I typically break up my text with linebreaks for readability, so I wouldn't want the linebreaks rendered in the document. That being, the
    tag is necessary.

    CSS whitespace,

    The html element

     is an element that, by default, uses CSS rule white-space: pre; so that the document's whitespace is rendered within that element. Normally, only a single space would be rendered regardless of the amount of whitespace in the document. A very good use of this style is for code blocks. If I write a code block, I will naturally space it as I intend and don't particularly care to wrap a 

    tag around each line (that's not semantic anyway) and I don't want to litter my code with
    either. Even worse, there's the code indentation! whitespace: pre makes a lot of sense here.

    function foo() {
      //etc
    }
    

    \r, \n, PHP_EOL

    Depending on the platform your php code is running from, you'll need to render a linebreak with \r, \n\, or both \r\n. PHP_EOL will automatically choose for you, so it is the most dynamic approach.

提交回复
热议问题