What does \n do php?

What are these called? \r, \n

Is there a tutorial that explains them?

What does n do php?

mkrieger1

15.7k4 gold badges46 silver badges57 bronze badges

asked Jul 18, 2010 at 7:52

1

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.)

answered Jul 18, 2010 at 7:53

Jon SkeetJon Skeet

1.4m835 gold badges8985 silver badges9091 bronze badges

\r is the carriage return

\n is the newline

These are available in many other languages aside from PHP.

answered Jul 18, 2010 at 7:53

Jacob RelkinJacob Relkin

158k32 gold badges341 silver badges318 bronze badges

0

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

answered Jul 18, 2010 at 7:53

What does n do php?

meder omuralievmeder omuraliev

180k70 gold badges383 silver badges427 bronze badges

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).

answered Jul 18, 2010 at 7:58

\r is a Carriage Return \n is a Line Feed (or new line).

On Windows systems these together make a newline (i.e. every time you press the enter button your fix will get a \r\n).

In PHP if you open a Windows style text file you will get \r\n at the end of paragraphs / lines were you've hit enter. If it was a Unix style text file you'd only get a \n.

answered Jul 18, 2010 at 7:56

OrenOren

3,2432 gold badges18 silver badges15 bronze badges

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

answered May 31, 2017 at 4:31

What does n do php?

Web7Web7

11612 bronze badges

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

answered May 17, 2018 at 7:52

What does n do php?

DushanDushan

5926 silver badges13 bronze badges

What is the meaning of \n in PHP?

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

Can we use \n in PHP?

It is an in-built function of PHP, which is used to insert the HTML line breaks before all newlines in the string. Although, we can also use PHP newline character \n or \r\n inside the source code to create the newline, but these line breaks will not be visible on the browser.

What is \n in a string?

Adding Newline Characters in a String. Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.

Is \n and \r same?

Both \n and \r are different characters and were used as end of line terminators in different operating systems. \r – was previously used as the end-of-line terminator in Mac text files, now \n is also used. \n\r -are used to terminate lines in the DOS and Windows text files.