32

I am using the PHP code:

$numberNewline = $number . '\n';
fwrite($file, $numberNewline);

to write $number to a file.

For some reason \n appears in the file. I am on a mac. What might be the problem?

3
  • 11
    Not sure, but I think \n only works if you use " not ' ('\n' is wrong while "\n" is right)
    – JCOC611
    Commented Feb 1, 2011 at 3:04
  • Just off the bat, Mac, Windows and *nix use different linefeeds (in general). Why, God only knows. It has driven me nuts. Do a search for newline, and see en.wikipedia.org/wiki/Newline. Commented Feb 1, 2011 at 3:06
  • Oh yes, and you need to use double quoted strings. Commented Feb 1, 2011 at 3:07

7 Answers 7

72

'\n' in single quotes is a literal \n.
"\n" in double quotes is interpreted as a line break.

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

0
45
$numberNewline = $number . "\n";
fwrite($file, $numberNewline);

Try this

4
  • 6
    For me this is not working even for new line. The only thing is working for me is "\r\n" with text file
    – Hiren Soni
    Commented Jun 3, 2013 at 14:01
  • 2
    It depends on the operating system of the server.
    – borayeris
    Commented Jun 4, 2013 at 19:53
  • okay. My development and production server both are different(OS is also differ), Can you please let me know which one should I use?
    – Hiren Soni
    Commented Jun 5, 2013 at 4:15
  • See chotesah's answer below
    – Carnix
    Commented Sep 8, 2015 at 19:21
36

If inserting "\n" does not yield any results, you can also try "\r\n" which adds a "carriage-return" and "new line."

0
26

Use PHP_EOL. PHP_EOL is platform-independent and good approach.

$numberNewline = $number .PHP_EOL;
fwrite($file, $numberNewline);

PHP_EOL is cross-platform-compatible(DOS/Mac/Unix).

2
  • 2
    For some reason I couldn't recall how to do this, and so I asked the Internet and landed here. As soon as I saw this answered I remembered. Duh. This, really, is the best one. The others work, perhaps, but this is the most correct answer.
    – Carnix
    Commented Sep 8, 2015 at 19:21
  • is it? it will save raw emails incorrectly on unix for example, not that perfect Commented Jan 13, 2020 at 17:23
1

The reason why you are not seeing a new line is because .txt files write its data like a stack. It starts writing from the beginning, then after it finishes, the blinking line (the one indicating where your next character is going to go) goes back to the beginning. So, your "\n" has to go in the beginning.

Instead of writing:

<?php
     $sampleLine = $variable . "\n";
     $fwrite($file, $sampleLine);
?>

You should write:

<?php
     $sampleLine = "\n" . $variable;
     $fwrite($file, $sampleLine);
?>
1

$numberNewline = $number . '\r\n';

fwrite($file, $numberNewline);

Try This

0

None of the above worked for me but it was so simple - here is the code... please use the KISS method.

echo file_put_contents("test.txt","\r\n \r\n$name \r\n$email \r\n$phone", FILE_APPEND);

It set a new blank line and then appends one line at a time.

Not the answer you're looking for? Browse other questions tagged or ask your own question.