使用fwrite不能在PHP中换行

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, "
");
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?

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

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

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