Forum Moderators: coopster

Message Too Old, No Replies

line breaks with PCs

         

someone

9:35 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



echo "Multiple\rtypes of\n\rline breaks\r\nhave been placed
within this string\n\nSee?";

gives


Multiple
types of

line breaks

have been placed
within this string

See?

How come \n\r gives one extra empty line, but \r\n only goes to the next line?

StupidScript

10:49 pm on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What kind of server are your running? Honestly, the result you posted doesn't look like normal behavior for either Windows or 'Nix servers.

AFAIK,

\n
means "newline" and
\r
means "carriage return" (what you get when you hit the Enter key while typing.)
\r
is typically only used by Windows implementations (i.e. running PHP on a Windows server) when it can't figure out what
\n
means.

On a Windows server, you can expect the

\n
newline code to be ignored and the
\r\n
carriage return PLUS newline to break the line once. Linux servers will ignore the
\r
carriage return when there is a
\n
newline present.

Testing on an Apache server running on Linux:

<?

echo "<pre>\n";

echo "New\nline<br />\n";

echo "Carriage\rreturn<br />\n";

echo "Both\r\ntypes<br />\n";

echo "</pre>\n";

?>

gives:

New

line

Carriage

return

Both

types

illustrating that the Linux implementation of PHP does indeed ignore the

\r
when the
\n
is present. Two
\n\n
produces a double linebreak, as expected.

[edited by: StupidScript at 10:55 pm (utc) on Aug. 11, 2006]

someone

11:02 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



The website runs on a linux-apache platform..

And that's the result I got when I looked at the webpage using a pc. same thing with a mac.

So the result doesn't make sense to you?

StupidScript

11:06 pm on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, it doesn't.

On the Linux platform, the

\r
should be ignored when there is a
\n
present. That's what my quick test proved.

Are you outputting to a text file, and that's where you are seeing the result?

I just used preformatted HTML (

<pre>
) to view it in my browsers (FF and MSIE both show the same result).

Using

\r\n
is just a way of coding so that if you move from a Linux box to a Windows box the code will work the same. Windows pays attention to the
\r
and Linux pays attention to the
\n
, OR a
\r
without an
\n
.

[edited by: StupidScript at 11:09 pm (utc) on Aug. 11, 2006]

someone

11:11 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



StupidScript,

can you run

echo "Multiple\rtypes of\n\rline breaks\r\nhave been placed
within this string\n\nSee?";

and see what kind of result you got?

StupidScript

11:22 pm on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Multiple

types of

line breaks

have been placed

within this string

See?

AND, I see what is going on ... just like I noted above, PLUS I learned something new, although I don't know how reliable it is, yet. ;)

The first break:

\r

Linux PHP is using it because there is no accompanying

\n
.

The second pair of breaks:

\n\r

Linux PHP is using the

\n
and then ALSO using the
\r
because there is no accompanying
\n
. Hang on ... the next one will explain it to you, I think ...

The third pair of breaks:

\r\n

Linux PHP is ignoring the

\r
because there IS an accompanying
\n
, which it is using.

The last pair of breaks:

\n\n

Linux PHP is using both of them.

The correct order is

\r
accompanied by
\n
, not the other way around. It's a "reading left-to-right" thing.

\n
FINISHES the instruction, so if there is a
\r
following it, that's a new instruction.

The part that I learned is that a normal Enter-type line break in the middle of an echo statement will ALSO produce a visible line break on a Linux PHP installation. Frankly I thought it would throw an error. I don't know if that can be counted on, though, so I'll keep using

\r\n
just to be safe.

someone

11:38 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



Thank you StupidScript! That makes totally sense to me now!