Appending files in PHP... can't get new line (\n) to work with variables

14,667

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.

Share:
14,667
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years
        $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?