php fwrite 文本文件转到下一行不起作用

php fwrite text file go to next line not work

我想保存到 txt 文件,但下一行不起作用

代码:

$text = '';
for(i=1;$i<10;$i++){
    $text .= $i."\n";
}

$file = fopen('text.txt','a');
fwrite($file,$text);
fclose($file);

您可以使用PHP_EOL 来跳转新行。而且您的代码无法正常工作,因为您错过了 i=1$ 标记。应该是$i=1

$text = '';
for($i=1;$i<10;$i++){
    $text .= $i.PHP_EOL;
}

$file = fopen('text.txt','a');
fwrite($file,$text);
fclose($file);

If you want to have a new, empty line at the beginning of a file you can simply initializes $text="\n";

  <?php

    $text      = '' . PHP_EOL;
    for($i=1;$i<10;$i++){
        $text .= $i . PHP_EOL;
    }

    $file = fopen('text.txt','a');
    fwrite($file,$text);
    fclose($file);