9

I'm trying to output a newline character in PHP that gets viewed in a web browser. I can only manage it by using the <br /> tag.

When I use \n, nothing occurs, so what is the benefit of using \n? Also what is the benefit of PHP_EOL? When I concatenate it to a string, just a space is printed not a newline.

Kenny Linsky
  • 1,726
  • 3
  • 17
  • 41
palAlaa
  • 9,500
  • 33
  • 107
  • 166
  • 5
    No matter what type of `EOL` you use it will be ignored in the browser unless inside a `pre` tag. Only a `br` or a block level element will make content got to the next line when rendered. – prodigitalson Jul 24 '11 at 23:59

4 Answers4

9

A web browser interprets the output of a PHP program as HTML, so \n and \r\n will not appear to do anything, just like inserting a newline in an HTML file. On the other hand, <br /> makes a new line in the interpreted HTML (hence "line BReak"). Therefore, <br /> will make new lines, whereas \r\n will not do anything.

Kenny Linsky
  • 1,726
  • 3
  • 17
  • 41
Chris
  • 1,569
  • 2
  • 11
  • 18
  • (it literally just separates the code into different line breaks) I can separate the code by making new lines manually , so the code is readable, till now I can't find any benefit for using \r\n – palAlaa Jul 25 '11 at 00:08
  • 4
    "\r\n" is useful for writing to files, php command line interfaces, socket writing, writing data to a textarea (HTML element), etc. It is basically useful for everything except direct HTML. – Chris Jul 25 '11 at 00:11
6

The PHP_EOL define is correct for the platform that you are on. So on windows PHP_EOL is \r\n on MAC it's \r on Linux, it's \n. Whereas <br /> or <br> is the HTML markup for line brake. If you're new to HTML & PHP, it's better to get a grasp of HTML first, then worry about PHP. Or start reading some source code, and run other peoples source code to see how they have done it. It will make you're code better just by copying their style. (Most of the time.)

Mark Tomlin
  • 8,593
  • 11
  • 57
  • 72
  • I make "\r\n " also no new line occurs. – palAlaa Jul 24 '11 at 23:59
  • I just want to know the new line I am talking about should be viewed in the HTML page or in another stream??? – palAlaa Jul 24 '11 at 23:59
  • Use `
    ` within HTML to make a new line on the webpage, use `PHP_EOL` to make a new line in the HTML Markup of the webpage. You are talking about two diffrent things, and you should know this, before you attempt HTML & PHP. You must learn HTML first, then learn PHP because one while works with the other, they are not the same thing.
    – Mark Tomlin Jul 25 '11 at 00:01
  • Can u tell me more about "PHP_EOL to make a new line in the HTML Markup of the webpage" or give me any references to read more about oi. – palAlaa Jul 25 '11 at 00:09
6

When you are using PHP to make a web app, there are a few layers involved:

  • Your PHP code, which outputs some data to
  • a web server, which transmits the data over the network to
  • a web browser, which parses the data and displays it on the screen.

Note that in the above, it is just data that is being passed along. In your case, that data is HTML, but it could just as easily be plain text or even a PNG formatted image. (This is one reason why you send a Content-Type: header, to specify the format of your data.)

Because it is so often used for HTML, PHP has a lot of HTML-specific features, but that's not the only format it can output. So, while a newline character is not always useful for HTML, is is useful:

  • if you want to format the HTML you are generating, not for the web browser, but for another person to be able to read;
  • if you want to generate plain text or another format where newline characters do matter.
benzado
  • 82,288
  • 22
  • 110
  • 138
3

PHP_EOL is useful when you're writing data to a file, example a log file. It will create line breaks specific to your platform.

luisdev
  • 558
  • 7
  • 22