Mohammad Mohammad - 1 month ago 20
PHP Question

php fwrite text file go to next line not work

I want save to txt file but pre line not work

Code:

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

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

Answer

You can use PHP_EOL for jump new line. And your code is not working, because you missed $ mark for i=1. It should be $i=1

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

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