New line in PHP output in a text file

You can use "\n" to write a new line.

$data = $_POST['name'] . "\n";
$data .= $_POST['email'] . "\n";
$data .= $_POST['message'] . "\n";

Just as a sidenote, \n has to be in doublequotes. When surrounded by single quotes it won't work.


As other answers state, you need to add in an end of line character after each field.

Different OS's use different line endings, though, and so a "\n" may not display as a new line on Windows, for example. As Mahdi said, you can use Windows style "\r\n" line endings, or you can use the PHP_EOL constant so that line endings appropriate to the server will be output, in which case your code would look like

$data = $_POST['name'] . PHP_EOL;
$data .= $_POST['email'] . PHP_EOL;
$data .= $_POST['message'] . PHP_EOL;