Pickle
Pickle

Reputation: 1124

Cannot line break in PHP using fwrite

I'm using this code to add user input to a text file:

<?php

//establish variables
$myFile = "chapter2.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$name = $_POST["name"];
$gnumber = $_POST["gnumber"];

fwrite($fh, $name); 
fwrite($fh, "\n");
fwrite($fh, $gnumber);

fclose($fh);

?>

However, it adds it to the text file like this:

namegnumber

Instead of this:

name

gnumber

Why does this happen?

Upvotes: 0

Views: 539

Answers (2)

Mike Mackintosh
Mike Mackintosh

Reputation: 14237

Sounds like you are viewing your output via HTML, in which case output will not be line broken with just \n.

fwrite($fh, "<br />".PHP_EOL);

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

There are a couple of things you can try. The main one being: try using fwrite($fh,PHP_EOL) instead of fwrite($fh,"\n");, as this will ensure your script generates the right line ending for the OS you're on.

Upvotes: 5

Related Questions