What are \r and \n meaning in PHP?

后端 未结 7 2062
难免孤独
难免孤独 2020-12-10 03:20

what are these called \\r \\n and is there a tutorial that explains them?

相关标签:
7条回答
  • 2020-12-10 03:38

    "\r" is Carriage return. Mind the double quotes when you use it with PHP! If you use single quotes, You will get the string \r instead of a line break.

    BTW, it is suitable to use "\r\n" if you want to put a line break in your text so it will be rendered correctly in different operating systems.

    • Mac: \r
    • Linux/Unix: \n
    • Windows: \r\n
    0 讨论(0)
  • \n is the newline or linefeed, other side \r is the carriage return. They differ in what uses them. Windows uses \r\n to signify the enter key was pressed, while Linux and Unix use \n to signify that the enter key was pressed.

    In Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special. In old Mac systems (pre-OS X), \r was the code for end-of-line instead. \r\n is the standard line-termination for text formats on the Internet.

    \r commands the carriage to go back leftwards until it hits the leftmost stop, \n commands the roller to roll up one line (a much faster operation). That's the reason you always have \r before \n, so that the roller can move while the carriage is still going leftwards.

    0 讨论(0)
  • 2020-12-10 03:41

    They're "carriage return" and "line feed" respectively. Typically on Windows, you need both together to represent a line terminator: "\r\n" whereas on most (all?) Unix systems, "\n" is enough.

    See the Wikipedia Newline entry for more details about the vagaries of different systems.

    See the PHP manual for more details about escape sequences in general, and the other ones available in PHP.

    Many other languages (e.g. C, C++, C#, Java, Perl, Python, Ruby) share the same escape sequences for carriage return and line feed - but they are all specified for the individual language. (In other words, it is language specific, but the answer would be the same for many languages.)

    0 讨论(0)
  • 2020-12-10 03:47

    \r is the carriage return

    \n is the newline

    These are available in many other languages aside from PHP.

    0 讨论(0)
  • 2020-12-10 03:50

    Not an answer to your question, but relevant nonetheless: I'd recommend using the PHP_EOL constant whenever you want to insert a new line. The PHP_EOL constant contains the correct new line character(s) for the platform on which the script is being run (\n on Unix, \r\n on Windows).

    0 讨论(0)
  • 2020-12-10 04:01

    They're escape sequences. \n is a newline and \r is a carriage return.

    In Windows most text editors have a newline as \r\n and on unix it's \n

    0 讨论(0)
提交回复
热议问题