[php] Writing a new line to file in PHP (line feed)

My code:

$i = 0;
$file = fopen('ids.txt', 'w');
foreach ($gemList as $gem)
{
    fwrite($file, $gem->getAttribute('id') . '\n');
    $gemIDs[$i] = $gem->getAttribute('id');
    $i++;
}
fclose($file);

For some reason, it's writing \n as a string, so the file looks like this:

40119\n40122\n40120\n42155\n36925\n45881\n42145\n45880

From Google'ing it tells me to use \r\n, but \r is a carriage return which doesn't seem to be what I want to do. I just want the file to look like this:

40119
40122
40120
42155
36925
45881
42145
45880

Thanks.

This question is related to php newline fopen fwrite linefeed

The answer is


Replace '\n' with "\n". The escape sequence is not recognized when you use '.

See the manual.

For the question of how to write line endings, see the note here. Basically, different operating systems have different conventions for line endings. Windows uses "\r\n", unix based operating systems use "\n". You should stick to one convention (I'd chose "\n") and open your file in binary mode (fopen should get "wb", not "w").


You can also use file_put_contents():

file_put_contents('ids.txt', implode("\n", $gemList) . "\n", FILE_APPEND);

PHP_EOL is a predefined constant in PHP since PHP 4.3.10 and PHP 5.0.2. See the manual posting:

Using this will save you extra coding on cross platform developments.

IE.

$data = 'some data'.PHP_EOL;
$fp = fopen('somefile', 'a');
fwrite($fp, $data);

If you looped through this twice you would see in 'somefile':

some data
some data

Use PHP_EOL which outputs \r\n or \n depending on the OS.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to newline

How can I insert a line break into a <Text> component in React Native? Print "\n" or newline characters as part of the output on terminal Using tr to replace newline with space How to write one new line in Bitbucket markdown? Line break in SSRS expression How to insert a new line in Linux shell script? Replace CRLF using powershell How to write new line character to a file in Java What is the newline character in the C language: \r or \n? How to print values separated by spaces instead of new lines in Python 2.7

Examples related to fopen

PHP - Failed to open stream : No such file or directory Getting an error "fopen': This function or variable may be unsafe." when compling Difference between r+ and w+ in fopen() Create a file if one doesn't exist - C PHP php_network_getaddresses: getaddrinfo failed: No such host is known PHP fopen() Error: failed to open stream: Permission denied Writing a new line to file in PHP (line feed) Unable to open a file with fopen() C fopen vs open fopen deprecated warning

Examples related to fwrite

PHP Excel Header Writing a new line to file in PHP (line feed)

Examples related to linefeed

What is the difference between a "line feed" and a "carriage return"? Carriage return and Line feed... Are both required in C#? In C#, what's the difference between \n and \r\n? What are carriage return, linefeed, and form feed? Writing a new line to file in PHP (line feed) Carriage Return\Line feed in Java