28

I am trying to write a PHP script that echos a string, but it doesn't recognize the end of line function in there.

For example,

echo "thank you \n "
echo "for coming over"

The \n is ignored and it prints the whole line as thank you for coming over.

I got the same result for the following to:

echo "thank you " . PHP_EOL.;
echo "for coming over" . PHP_EOL.;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ismael
  • 343
  • 1
  • 4
  • 7
  • 7
    Try inserting `
    `.
    – Tomalak Jun 29 '11 at 11:36
  • 2
    Look at your source code, you'll see that the new line has been inserted. It's just that the browser ignores new lines. – dee-see Jun 29 '11 at 11:37
  • possible duplicate of [When do I use the PHP constant "PHP_EOL"?](http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol) – Shakti Singh Jun 29 '11 at 11:38
  • Are you outputting to a browser, or to a command line (or somewhere else)? Because browsers ignore whitespace, including newlines, except in certain tags or when certain styles are present. You need to clarify your question. Most of the answers below assume you are outputting to browser. – Aaron Wallentine Sep 30 '16 at 16:19
  • Why the dot after `PHP_EOL`? – Peter Mortensen Jul 07 '19 at 16:38

5 Answers5

25

There are different ways to solve that:

Using Pre-formatted text

echo '<pre>'; // pre-formatted text, \n should work
echo "thank you \n ";
echo "for coming over";
echo '</pre>';

Changing the content type

header('Content-Type: text/plain');
echo "thank you \n ";
echo "for coming over";

You browser will now properly read the output as text.

Using the HTML break element

echo "thank you <br>\n ";
echo "for coming over";

So output always depends on what you want to output. Is it text? Is it HTML? Is it some text within HTML? Depending on what you need you should take care on the format and formatting.

middus
  • 9,103
  • 1
  • 31
  • 33
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I just fixed your code (missing ; and ), hope it's okay. – middus Jul 22 '11 at 17:55
  • 2
    Related to *Using Pre-formatted text*, [`white-space`](https://developer.mozilla.org/en-US/docs/Web/CSS/white-space) css property can be used too. – Oriol Aug 31 '13 at 17:34
17

If you are doing this in a browser, replace \n with <br />. The file line breaks are not rendered in an HTML page, unless specified via CSS or if they're enclosed in certain tags. You could also optionally change the file type to text/plain, but I don't think that would be desired.

10

try this normally you can use

 echo "thank you <br>";
 echo "for coming over<br>";

and at time of file handling

echo "thank you ".PHP_EOL."";
echo "for coming over".PHP_EOL."";
user948319
  • 356
  • 2
  • 9
3

If you want to print text containing eol's in html, give nl2br a shot

$str = "thank you \n ";
$str .= "for coming over";
nl2br($str);

It will add HTML line breaks before all eols.

middus
  • 9,103
  • 1
  • 31
  • 33
0

Check the source of the web page from browser, there will be a new line. Since in HTML, whitespaces like newline, tabs or spaces are ignored, you can't see them in the browser. You need to use <br /> or &nbsp; etc. to print what you want.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
balki
  • 26,394
  • 30
  • 105
  • 151