This is the code.I don't know where I have made a mistake.I tried with single quote as well. I tried it but all items come in one line.
$output_line=$item."\n";
fwrite($myfile,$output_line);
This is the code.I don't know where I have made a mistake.I tried with single quote as well. I tried it but all items come in one line.
$output_line=$item."\n";
fwrite($myfile,$output_line);
Use PHP_EOL
instead of \n
.
$output_line=$item . PHP_EOL ;
Use PHP_EOL which produces \r\n
or \n
$data = 'some data' . PHP_EOL . 'this should be on new line';
$fp = fopen('my_file', 'a');
fwrite($fp, $data);
// File output
some data
this should be on new line
Line break representation can be different on different operating system. In PHP there is an predefined constant called PHP_EOL which return the correct 'End Of Line' symbol for current platform. Available since PHP 5.0.2.
Therefore, your code should be as below:
$output_line=$item.PHP_EOL;
fwrite($myfile,$output_line);
You need to use implode to add a "text" between each item.
Implode takes an array and adds a string in between each item and makes it all string.
$output_line=implode(" PHP_EOL", $item);
fwrite($myfile,$output_line);
This will now make:
Item1 PHP_EOL
Item2 PHP_EOL
Item3 PHP_EOL