Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making new line in php write to file

Tags:

php

fwrite

I tried this but wont work

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
?> 

This is how it displays in my newfile.txt:

Mickey MouseMinnie Mouse`

but I want it like this:

Mickey Mouse
Minnie Mouse
like image 269
Ghostff Avatar asked Mar 19 '23 05:03

Ghostff


1 Answers

It is the best to use PHP_EOL. It's cross-platform and will automatically choose the correct newline character(s) for the platform PHP is running on.

$txt = "Goofy".PHP_EOL;
like image 121
Marcus Rommel Avatar answered Mar 26 '23 02:03

Marcus Rommel