Appending files in PHP... can't get new line (\n) to work with variables
开发者_运维技巧 $file = "file.txt";
$f = fopen($file, 'a');
fwrite($f, $usrname . "\n");
fclose($f);
Whenever I run this code, I want to have the person's username($usrname) append to file.txt. However, when I run this, it doesn't add a new line. What am I doing wrong?
Instead of hard-coding the newline, you can use the standard PHP_EOL
constant, which is the end-of-line marker for the platform you're executing the script on:
fwrite($f, $usrname . PHP_EOL);
Of course, if you want the file to be portable to other operating systems, then you should pick an EOL marker and stick with it, ignoring what the host platform uses.
精彩评论