PHP difference between \r\n and \n

Asked
Active3 hr before
Viewed126 times

6 Answers

difference
90%

The following are the major differences at these end of line characters: ,UNIX systems use \n as their end of line character. This line break is not visible on the browser. \n is used for all other systems, applications, and the Internet. ,How to remove the first character of string in PHP?,\n:UNIX systems use \n as their end of line character. This line break is not visible on the browser. \n is used for all other systems, applications, and the Internet. 

load more v
88%

\r\n are end of line characters for Windows systems.,\n is the end of line character for UNIX systems.,These characters are invisible. From my own experience \n is usually okay for Windows as well. ,Some prefer to use PHP_EOL constant instead of these characters for portability between platforms.

Some prefer to use PHP_EOL constant instead of these characters for portability between platforms.

echo 'hi'.PHP_EOL;
echo "hi\n";

$headers = "From: webmaster@example.com".PHP_EOL
   .
"Reply-To: webmaster@example.com";
72%

Yes yes, I am aware that '\n' writes a newline in UNIX while for Windows there is the two character sequence: '\r\n'. All this is very nice in theory, but my question is why? Why the carriage return character is extra in Windows? If UNIX can do it in \n why does it take Windows two characters to do this? ,For example, on Windows, writing the character '\n' actually outputs the two- character sequence '\r\n' (and when reading the file back, '\r\n' is translated back into a single '\n' character).,Connect and share knowledge within a single location that is structured and easy to search., 1 There are very few Windows programs that actually require CRLF. CRLF may be the default, but nearly everything will autodetect and use LF just fine. I have all my text editors on Windows configured to use LFs for all new files, and it's really not an issue. – Kevin Feb 15 '17 at 15:26

I don't know if this is common knowledge, but it should be noted that CR is still understood by modern terminal emulators:

$ printf "hey world\rsup\n"
sup world

It's handy for progress indicators, e.g.

for i in {
   1. .100
}
do
   printf "\rLoading... %d%%"
$i
sleep 0.01
done
echo
load more v
65%

nl2br — Inserts HTML line breaks before all newlines in a string, Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r). , Whether to use XHTML compatible line breaks or not. , Returns the altered string.

foo isn't<br />
bar
load more v
75%

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.,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.,Is this website helpful to you? Please give us a like, or share your feedback to help us improve. Connect with us on Facebook and Twitter for the latest updates.,Let's take a look at the following example to understand how it basically works:

<?php
echo "If you view the page source \r\n you will find a newline in this string.";
echo "<br>";
echo nl2br("You will find the \n newlines in this string \r\n on the browser window.");
?>
40%

The following basic scripts do not render a new line in my Firefox browser., What happens to my candy-creatures when they get wet and how do I stop it? ,Asking for help, clarification, or responding to other answers., @Dan I don't see general coding questions defined as off topic in the help center. askubuntu.com/help/on-topic – mchid Aug 8 '19 at 12:32

A new line inside the html source code is not displayed as a newline in a browser.
Take a look at the source code of your generated html pages, you will find your newlines

The html code of a new line is <br>

In short

<?php echo "\r\n"; ?> // Writes a new line in the output stream
<?php echo "<br>"; ?> // Writes the newline html code

Another example:

<php echo "<html><body>A<br>\r\nB<br>C\r\n</body></html>" ?>

Will output the raw text:

<html>

<body>A<br>
   B<br>C
</body>

</html>

A browser will display this

A
B
C
load more v

Other "difference-undefined" queries related to "PHP difference between \r\n and \n"