Search code examples
phphtmlsyntaxlinefeed

What does exactly mean by 'Line feeds' in HTML and PHP? How do they are added in HTML and PHP code?


I was reading PHP Manual and I come across following text paragraph :

Line feeds have little meaning in HTML, however it is still a good idea to make your HTML look nice and clean by putting line feeds in. A linefeed that follows immediately after a closing ?> will be removed by PHP. This can be extremely useful when you are putting in many blocks of PHP or include files containing PHP that aren't supposed to output anything. At the same time it can be a bit confusing. You can put a space after the closing ?> to force a space and a line feed to be output, or you can put an explicit line feed in the last echo/print from within your PHP block.

I've following questions related to the text from above paragraph :

  1. What does exactly mean by 'Line feeds' in HTML?
  2. How to add them to the HTML code as well as PHP code and make visible in a web browser? What HTML entities/tags/characters are used to achieve this?
  3. Is the meaning of 'Line feed' same in case of HTML and PHP? If no, what's the difference in meaning in both the contexts?
  4. Why the PHP manual is saying in first line of paragraph itself that? What does PHP Manual want to say by the below sentence?

"Line feeds have little meaning in HTML"

  1. How can it be useful to remove a linefeed that follows immediately after a closing tag ?> when someone is putting in many blocks of PHP or include files containing PHP that aren't supposed to output anything?

Please someone clear my above mentioned doubts by giving answer in simple, lucid and easy to understand language. If someone could accompany the answer by suitable working code examples it would be of great help to me in understanding the concept more clearly.

Thank You.


Solution

  • What does exactly mean by 'Line feeds' in HTML?

    It is a general computing term.

    The character (0x0a in ASCII) which advances the paper by one line in a teletype or printer, or moves the cursor to the next line on a display.

    source: Wiktionary

    How to add them to the HTML code

    Press the enter key on your keyboard. Note that (with a couple of exceptions like <pre>) all whitespace characters are interchangeable in HTML. A new line will be treated as a space.

    as well as PHP code

    Ditto … or you could use the escape sequence \n inside a string literal.

    and make visible in a web browser?

    The material you quoted is talking about making source code look nice. You generally don't want line feed characters to be visible in a browser.

    You could use a <pre> element instead.

    Outside of <pre> elements (and the CSS setting they have by default) you can use a space instead of a new line for the same effect in HTML.

    What HTML entities/tags/characters are used to achieve this?

    &#10;

    … but the advice given in the last sentence of the material you quoted is probably a better approach.