PHP newline not working in text file

后端 未结 7 418
无人共我
无人共我 2021-02-02 08:41

I am using the PHP code:

$numberNewline = $number . \'\\n\';
fwrite($file, $numberNewline);

to write $number to a file.

For some reason

相关标签:
7条回答
  • 2021-02-02 08:52

    $numberNewline = $number . '\r\n';

    fwrite($file, $numberNewline);

    Try This

    0 讨论(0)
  • 2021-02-02 08:56
    $numberNewline = $number . "\n";
    fwrite($file, $numberNewline);
    

    Try this

    0 讨论(0)
  • 2021-02-02 09:04

    The reason why you are not seeing a new line is because .txt files write its data like a stack. It starts writing from the beginning, then after it finishes, the blinking line (the one indicating where your next character is going to go) goes back to the beginning. So, your "\n" has to go in the beginning.

    Instead of writing:

    <?php
         $sampleLine = $variable . "\n";
         $fwrite($file, $sampleLine);
    ?>
    

    You should write:

    <?php
         $sampleLine = "\n" . $variable;
         $fwrite($file, $sampleLine);
    ?>
    
    0 讨论(0)
  • 2021-02-02 09:06

    If inserting "\n" does not yield any results, you can also try "\r\n" which adds a "carriage-return" and "new line."

    0 讨论(0)
  • 2021-02-02 09:07

    Use PHP_EOL. PHP_EOL is platform-independent and good approach.

    $numberNewline = $number .PHP_EOL;
    fwrite($file, $numberNewline);
    

    PHP_EOL is cross-platform-compatible(DOS/Mac/Unix).

    0 讨论(0)
  • 2021-02-02 09:09

    None of the above worked for me but it was so simple - here is the code... please use the KISS method.

    echo file_put_contents("test.txt","\r\n \r\n$name \r\n$email \r\n$phone", FILE_APPEND);
    

    It set a new blank line and then appends one line at a time.

    0 讨论(0)
提交回复
热议问题