Search code examples
phpposthandle

"\n" in php is not moving down a line in my file


I'm writing a small php script to save form data to a file and everything is working fine, except \n isn't moving down a line in the file. It's printing all data on the same line.

<?php

$firstname = $_POST["firstname"];
$email = $_POST["email"];
$handle = fopen('details.txt','a');
fwrite($handle, $firstname ."\n");
fwrite($handle, $email);
?>
<br>
 Your email address is: <?php
 echo $_POST["email"];
?>

Solution

  • Must be cross-platform line break issue. Your system might be expecting \r\n

    Use PHP_EOL

    The correct 'End Of Line' symbol for this platform. Available since PHP 5.0.2

    This makes your new line cross platform. You don't have to worry about \n or \r\n anymore.

    fwrite($handle, $firstname .PHP_EOL);