Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP newline not working 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?

like image 978
JJJollyjim Avatar asked Feb 01 '11 03:02

JJJollyjim


People also ask

How do you make a new line of text 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 to insert line break in PHP?

The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.

How to break line in echo PHP?

Answer: In PHP to echo a new line or line break use '\r\n' or '\n' or PHP_EOL. '\n' or '\r\n' is the easiest way to embed newlines in a PHP string. Also, '\n' can be used to create line breaks when creating PHP text files.


1 Answers

'\n' in single quotes is a literal \n.
"\n" in double quotes is interpreted as a line break.

http://php.net/manual/en/language.types.string.php

like image 52
deceze Avatar answered Sep 22 '22 17:09

deceze