Php new line in text file

I am using the PHP code:

$numberNewline = $number . '\n';
fwrite[$file, $numberNewline];

to write $number to a file.

For some reason \n appears in the file. I am on a mac. What might be the problem?

asked Feb 1, 2011 at 3:02

3

$numberNewline = $number . "\n";
fwrite[$file, $numberNewline];

Try this

answered Feb 1, 2011 at 3:04

borayerisborayeris

2,5431 gold badge26 silver badges31 bronze badges

4

If inserting "\n" does not yield any results, you can also try "\r\n" which adds a "carriage-return" and "new line."

answered Feb 1, 2011 at 3:07

0

Use PHP_EOL. PHP_EOL is platform-independent and good approach.

$numberNewline = $number .PHP_EOL;
fwrite[$file, $numberNewline];

PHP_EOL is cross-platform-compatible[DOS/Mac/Unix].

answered Jun 4, 2015 at 11:54

vineetvineet

13.2k10 gold badges53 silver badges75 bronze badges

2

The reason why you are not seeing a new line is because .txt files write its data like a stack. It starts writing from the beginning, then after it finishes, the blinking line [the one indicating where your next character is going to go] goes back to the beginning. So, your "\n" has to go in the beginning.

Instead of writing:


You should write:


answered Apr 23, 2013 at 14:20

None of the above worked for me but it was so simple - here is the code... please use the KISS method.

echo file_put_contents["test.txt","\r\n \r\n$name \r\n$email \r\n$phone", FILE_APPEND];

It set a new blank line and then appends one line at a time.

Koopakiller

2,7403 gold badges31 silver badges46 bronze badges

answered Aug 8, 2015 at 23:10

$numberNewline = $number . '\r\n';

fwrite[$file, $numberNewline];

Try This

answered Jan 7, 2017 at 5:46

Topic: PHP / MySQLPrev|Next

Answer: Use the Newline Characters '\n' or '\r\n'

You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br[] function which inserts HTML line breaks before all newlines in a string.

Let's take a look at the following example to understand how it basically works:

Note: The character \n writes a newline in UNIX while for Windows there is the two character sequence: \r\n. To be on safe side use the \r\n instead.

Related FAQ

Here are some more FAQ related to this topic:

  • How to combine two strings in PHP
  • How to remove white space from a string in PHP
  • How to write comments in PHP

How do you add a new line to a text file in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br[] function which inserts HTML line breaks before all newlines in a string.

How do you make a new line in a text file?

You put the slash for the newline character backwards. It should be: "\n".

What is Br in PHP?

Example. Insert line breaks where newlines [\n] occur in the string: echo nl2br["One line. \nAnother line."];

How do you make a new line in Python?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line.

Chủ Đề