file_put_contents, file_append and line breaks file_put_contents, file_append and line breaks php php

file_put_contents, file_append and line breaks


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 newlineYou could try this

A file with EOL after the last number looks like:1_2_3_EOFbut a file without that last character looks like1_2_3EOFwhere _ 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 ) {    var_dump($line);}

...tricky


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 systemsthe 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

\r\n

insted of \n