phpfopenfwrite

PHP file write with a new line


I'm making a chat room with a text file. Very simple, but I don't want it to put a damper on the server. Anyways. When I try to make a message, it puts the appended message on the same line like this

Alan,5/6/12,message<br>Joe,5/6/12,hello<br>

I'd like it to be

Alan,5/6/12,message
Joe,5/6/12,hello

This is what I have.

$total = 'Alan,5/6/12,hello joe<br>\r\n';
$fn = "messages.txt";
$file = fopen($fn,"a");
fwrite($fn,$total);
fclose($fn);

Thanks.


Solution

  • $total = 'Alan,5/6/12,hello joe<br>\r\n';
    

    would have worked if you'd used doublequotes, e.g.

    $total = "Alan,5/6/12,hello joe<br>\r\n";
             ^--                           ^--
    

    single-quoted strings do not interpret metacharacters like linebreak/newline, and all your single-quoted version does is put a literal r and n character into the output.