37

For some reason I can't use \n to create a linefeed when outputting to a file with PHP. It just writes "\n" to the file. I've tried using "\\n" as well, where it just writes "\n" (as expected). But I can't for the life of me figure out why adding \n to my strings isn't creating new lines. I've also tried \r\n but it just appends "\r\n" to the line in the file.

Example:

error_log('test\n', 3, 'error.log');
error_log('test2\n', 3, 'error.log');

Outputs:

test\ntest2\n

Using MAMP on OSX in case that matters (some sort of PHP config thing maybe?).

Any suggestions?

Lizard
  • 43,732
  • 39
  • 106
  • 167
ggutenberg
  • 6,880
  • 8
  • 39
  • 48

11 Answers11

61

Use double quotes. "test\n" will work just fine (Or, use 'test' . PHP_EOL).

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:

http://php.net/manual/en/language.types.string.php

Top-Master
  • 7,611
  • 5
  • 39
  • 71
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • using single quotes does not change the value into entities so you have to use double! +1 – RobertPitt Jun 10 '10 at 15:53
  • 7
    How could you give such a complete answer in less than 2 minutes? That's the time I take to **read** the question alone! :o – cregox Apr 14 '11 at 19:10
29

\n is not meant to be seen as a new line by the end user, you must use the html <br/> element for that. /n only affects how the html that is generated by php appears in the source code of the web page. if you go to your web page and click on 'view source' you will see php-generated html as one long line. Not pretty. That's what \n is for ; to break that php-generated html into shorter lines. The purpose of \n is to make a prettier 'view source' page.

aldrin27
  • 3,407
  • 3
  • 29
  • 43
Huraiby
  • 291
  • 3
  • 3
15

When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you’re using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you’ll see that your line feeds are present.

<?php
echo "Line 1\nLine 2";
?>

This will render in your browser as:

Line 1 Line 2

If you need to send plain text to your browser, you can use something like:

<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>

This will output:

Line 1
Line 2
nilesh
  • 723
  • 1
  • 8
  • 24
7

nl2br() function use for create new line

echo nl2br("Welcome\r\n This is my HTML document", false);

The above example will output:

Welcome
This is my HTML document

ScottJShea
  • 7,041
  • 11
  • 44
  • 67
Pankaj Monga
  • 179
  • 2
  • 8
6

I'm pretty sure you are outputting to a html file. The problem is html ignores newlines in source which means you have to replace the newlines with <br/> if you want a newline in the resulting page display.

Tariqulazam
  • 4,535
  • 1
  • 34
  • 42
Souhaieb Besbes
  • 1,485
  • 17
  • 30
4

You need to use double quotes. Double quotes have more escape chars.

error_log("test\n", 3, 'error.log');
error_log("test2\n", 3, 'error.log');
Max
  • 23
  • 4
oshirowanen
  • 61
  • 1
  • 3
3

to place the \n in double quotes try


$LOG = str_replace('\n', "\n", $LOG);
Lal
  • 14,726
  • 4
  • 45
  • 70
aaron
  • 31
  • 2
2

It's because you use apostrophes ('). Use quotationmarks (") instead. ' prompts PHP to use whatever is in between the apostrophes literally.

Jan K.
  • 1,607
  • 2
  • 13
  • 22
2

Double quotes are what you want. Single quotes ignore the \ escape. Double quotes will also evaluate variable expressions for you.

Check this page in the php manual for more.

Matt Stephenson
  • 8,442
  • 1
  • 19
  • 19
1

The “\n” or “\r” or similar tags are treated as white-space in HTML and browsers. You can use the "pre" tag to solve that issue

<?php
    echo "<pre>";
    echo "line1 \n some text \t a tab \r some other content";
    echo "</pre>";
?>
0

If you want to print something like this with a newline (\n) after it:

<p id = "theyateme">Did it get eaten?</p>

To print the above, you should do this:

<?php
  print('<p id = "theyateme">Did it get eaten?</p>' . "\n");
?>

The client code from above would be:

<p id = "theyateme">Did it get eaten?</p>

The output from above would be:

Did it get eaten?

I know it's hard, but I always do it that way, and you almost always have to do it that way.

Sometimes you want PHP to print \n to the page instead of giving a newline, like in JavaScript code (generated by PHP).

NOTE about answer: You might be like: Why did you use print instead of echo (I like my echo). That is because I prefer print over echo and printf, because it works better in some cases (my cases usually), but it can be done fine with echo in this case.

Ewer Ling
  • 119
  • 4
  • 15