Home » Php » php – file_put_contents, file_append and line breaks

php – file_put_contents, file_append and line breaks

Posted by: admin April 23, 2020 Leave a comment


I’m writing a PHP script that adds numbers into a text file. I want to have one number on every line, like this:


If I use file_put_contents($filename, $commentnumber, FILE_APPEND), the result looks like:


If I add a line break like file_put_contents($filename, $commentnumber . "\n", FILE_APPEND), spaces are added after each number and one empty line at the end (underscore represents spaces):


How do I get that function to add the numbers the way I want, without spaces?

How to&Answers:

Did you tried with PHP EOL constant?

file_put_contents($filename, $commentnumber . PHP_EOL, FILE_APPEND)

— Added —

I just realize that my file editor does the same, but don’t worrie, is just a ghost character that the editor places there to signal that there is a newline
You could try this

A file with EOL after the last number looks like:

but a file without that last character looks like


where _ means a space character

You could try to parse the file contents using php to see what’s inside

$lines = explode( PHP_EOL, file_get_contents($file));
foreach($lines as $line ) {



pauls answer has the correct approach but he has a mistake.
what you need ist the following:

file_put_contents($filename, trim($commentnumber).PHP_EOL, FILE_APPEND);

the PHP_EOL constant makes sure to use the right line ending on mac, windows and unix systems
the trim function removes any newline or whitespace on both sides of the string.
converting to integer would be a huge mistake because
1. you might end up having zero, expecially because of white space or special characters (wherever they come from…)
2. ids dont necessarily need to be integers


Ohh Guys! Just Use


insted of \n


There is nothing in the code you provided that would generate those spaces, unless $commentnumber already contains the space to begin with. If that is the case, simply use trim($commentnumber) instead.

There is also nothing in your code that would explain empty lines at the bottom of the file, unless $commentnumber can be an empty string. If that is the case, and you want it to output the number 0 instead, use intval($commentnumber).

Of course, you need only one of those two. If you want to preserve string-like content, use trim(); if you always want integers, use intval(), which already trims it automatically.

It is also possible that you accidentally wrote " \n" instead of "\n" in your actual code, but in the code you posted here it is correct.


annoyingregistration, what you have there is absolutely fine.
PHP_EOL and “\n” are exactly the same.

The code you provided theres nothing wrong with it so it must be the value of $commentnumber that has a space at the end of it. as stated, run your $commentnumber through the trim() function.

file_put_contents($filename, trim($commentnumber . "\n"), FILE_APPEND);

Good luck.


After reading your code and responses, I have come up with a theory…

Since I can’t see that there’s anything wrong with your code, how did you open and read the file? Did you actually open it in a text editor? Did you use a PHP script to do it? If so, open the file with a text editor and check that there are actually spaces at the end of each line. If there is actually is…well, ignore the rest of this answer, then. If not, just read on.

For instance, if you use something like this:

$lines = file($filename);
if($lines) // Error reading
foreach($lines as $line)
  echo $line."<br />";

Then you would always a whitespace at the end of the line because of the way file() work. Make sure each $line does not have a whitespace – such as a newline character – at the end.
Since HTML handles all whitespaces – spaces, tabs, newlines etc. – as spaces, if there is a whitespace at the end of $line, then those would appear as spaces in the HTML output.

Solution: use rtrim($line) to remove whitespaces at the end of the lines. Using the following code:

$lines = file($filename);
if($lines) // Error reading
foreach($lines as $line)
  echo rtrim($line)."<br />";

wouldn’t have the same problems as the first example, and all spaces at the end of the lines would be gone.


its because each time you write to the file, the file is being finished, file_put_contents inserts an extra line break at the end