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.
- \r\n :
Windows systems use \r\n as their end of line character. Therefore, the carriage return character is extra in the case of Windows Operating Systems. Windows machines output a newline character in the form of a two-character sequence because of backward compatibility. This line break is not visible on the browser. In order to visualize these on the browser, the nl2br() method can be used in PHP. However, \n can also be used in Windows systems. \r\n is the standard line-termination for text formats on the Internet. \r\n is only used on Windows Notepad, the DOS command line, most of the Windows API, and in some (older) Windows apps.
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.
PHP code: The following code snippet illustrates the usage of line separators.
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.