php \r and \n same thing?
This has nothing to do with PHP, and is a consequence of history :
- UNIX / Linux use
\n
for linebreaks - Mac (before OSX) used
\r
- And windows uses a combinaison of both
PHP has just kept that behavior -- so it can work with those different OSes and their files.
Also, note :
- Those are not functions : they are (special) characters
- They are not exactly the same thing :
\r
is Carriage return\n
is Newline
- and, btw, those are another consequence of history : look at their names, and think about typewriters ;-)
How are \r \t and \n different from one another?
\n
is a symbol for new line
\t
is a symbol for tab
and \r
is for 'return'
You can find more information here: What is the difference between \r and \n?
what is the meanig and use of \n\t\t\t\t\ in php
They're escape sequences. \r
represents the carriage return, \n
represents the newline (linefeed) and \t
represents the tab.
Is a new line = \n OR \r\n?
\n
is used for Unix systems (including Linux, and OSX).
\r\n
is mainly used on Windows.
\r
is used on really old Macs.
PHP_EOL
constant is used instead of these characters for portability between platforms.
What is the difference between \r and \n?
They're different characters. \r
is carriage return, and \n
is line feed.
On "old" printers, \r
sent the print head back to the start of the line, and \n
advanced the paper by one line. Both were therefore necessary to start printing on the next line.
Obviously that's somewhat irrelevant now, although depending on the console you may still be able to use \r
to move to the start of the line and overwrite the existing text.
More importantly, Unix tends to use \n
as a line separator; Windows tends to use \r\n
as a line separator and Macs (up to OS 9) used to use \r
as the line separator. (Mac OS X is Unix-y, so uses \n
instead; there may be some compatibility situations where \r
is used instead though.)
For more information, see the Wikipedia newline article.
EDIT: This is language-sensitive. In C# and Java, for example, \n
always means Unicode U+000A, which is defined as line feed. In C and C++ the water is somewhat muddier, as the meaning is platform-specific. See comments for details.
What is the benefit of \n and PHP_EOL in PHP?
A web browser interprets the output of a PHP program as HTML, so \n
and \r\n
will not appear to do anything, just like inserting a newline in an HTML file. On the other hand, <br />
makes a new line in the interpreted HTML (hence "line BReak"). Therefore, <br />
will make new lines, whereas \r\n
will not do anything.
Difference between \r and \n
\r
is "Carriage Return" (CR, ASCII character 13), \n
is "Line Feed" (LF, ASCII character 10). Back in the days, you had two ASCII characters at the end of each line to tell a printer what to do - CR would tell the printer to go back to the left edge of the paper, LF would advance to the next line.
Operating systems still have different conventions as to what the end of a line looks like -- some of them have \n\r
, some have \n
, some have \r\n
.
In Javascript, you mostly deal with \n
- this is how strings are typically switching to the next line. However, depending on what strings you are working with, you may be encountering \r
as well.
Related Topics
Maximum Length of Generated Hash When Using Password_Hash
Echo PHP Variable from JavaScript
PHP Function to Replace a (I)Th-Position Character
PHP Session Id Changing on Every Request
Class Firebase\Jwt\Jwt Not Found
Laravel Form Methods VS Traditional Coding
How to Join Three Tables in Codeigniter
Crypt for Password Hashing. Blowfish Produces Weird Output
Reply-To Address in PHP Contact Form
Ssl Error Can Not Change to Tls
How to Get a List of Databases
Query on a Many-To-Many Relationship Using Doctrine with Symfony2
Extending Session Timeout in PHP via the .Htaccess
Quoting Constants in PHP: "This Is a My_Constant"
How to Disable Adding Properties into a Class from an Instance of the Class
Gmail API: 400 Bad Request When Trying to Send an Email (PHP Code)