-2

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);

4 Answers4

0

Use PHP_EOL instead of \n.

 $output_line=$item . PHP_EOL ;
Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
0

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
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
0

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);
Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
Lalmani Dewangan
  • 306
  • 2
  • 11
0

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
Andreas
  • 23,610
  • 6
  • 30
  • 62