Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php \r and \n same thing?

Tags:

php

Why have 2 functions that does the same thing? They both add a linebreak.

Any diffrence between them?

like image 702
Sosa Avatar asked Jan 07 '10 06:01

Sosa


People also ask

Is \r the same as \n?

The /r stands for return or carriage return which owes it's history to the typewriter. A carriage return moved your carriage all the way to the right so you were typing at the start of the line. The /n stands for new line, again, from typewriter days you moved down to a new line.

What does \r mean in PHP?

"\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.

What does \r and \n mean?

\n means new line. It means that the cursor must go to the next line. \r means carriage return. It means that the cursor should go back to the beginning of the line.

What is the \n used for in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' 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.


1 Answers

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 ;-)
like image 65
Pascal MARTIN Avatar answered Oct 02 '22 04:10

Pascal MARTIN