Open In App

What’s the difference between \n and \r\n in PHP ?

There are many types of line-ending characters that are used in PHP. They differ depending upon the operating system and editors using them. It’s also based on how apps, libraries, protocols, and file formats deal with things. These characters are invisible. \n is used for the newline or linefeed, whereas \r is the carriage return. They differ in what uses them, that is language-dependency holds. However, portability across the platforms is not maintained upon the usage of these EOL characters, which can be maintained by using PHP_EOL constant.

Linux/Unix: \n



Windows: \r\n

PHP code: The following code snippet illustrates the usage of line separators.




<?php
  #declaring a string
  $str = "Illustrating the usage \r\nof\n\rline\nseparators\r";
  #printing string
  echo nl2br($str);
?>

Output
Illustrating the usage <br />
of<br />

line<br />
separators<br />

Explanation: The functionality of both the line-separators is the same on the browser. 

The following are the major differences at these end of line characters: 

\r\n \n
Compatible in Windows-based systems. Compatible in Unix/Linux based systems.
Strictly for Linux systems. Can be used for Windows as well.
Also known as the carriage-return/line-feed pair (CRLF). Also known as the standard line break. 
It is a double-character literal sequence. ‘\n’ is a character constant representing a single character, the newline character.
Article Tags :