\n does apparently not work

Post your questions and problem reports here

Moderator: kfury77

Forum rules
Please try to follow these guidelines. This will help to receive faster and more accurate response.
  • Check the Support section of the corresponding product first. Chances are you will find your answer there;
  • Do not create new topics for already reported problems. Add your comments to the existing topics instead;
  • Create separate topic for each problem request. Do NOT post a number of non-related problem reports in a single topic;
  • Give your topic a meaningful title. Titles such as "A question," "Bug report" and "Help!" provide others no clue what your message is about;
  • Include the version number of the software you are using;
  • This is not an official customer support helpdesk. If you need a prompt and official response, please contact our support team directly instead. It may take a while until you receive a reply in the forum;
Post Reply
larshgf
Posts: 8
Joined: Fri Nov 03, 2006 12:29 pm
Location: Denmark

\n does apparently not work

Post by larshgf »

Hello,

When I write this (inside php tags off course)...

echo "Hello, World\n" ;
echo "Hello, Giannis\n" ;

I get this result...
Hello, WorldHello, Giannis

I would expect...
Hello, World
Hello, Giannis

It seems that my escape char for new line (\n) is not working properly? Something wrong with "rpidPHP" or my coding???

BR
Lars
User avatar
MikeyB
Top Contributor
Posts: 511
Joined: Fri Jun 09, 2006 10:38 am
Location: UK
Contact:

Post by MikeyB »

Hi,

I don't know much about PHP, but is it's the same as ASP that is what I'd kind of expect to see.

I would try:

echo "Hello, World<br>" ;
echo "Hello, Giannis<br>" ;

Browsers will treat new lines in HTML code as spaces rather than what you'd expect.
larshgf
Posts: 8
Joined: Fri Nov 03, 2006 12:29 pm
Location: Denmark

Post by larshgf »

Thank you Mickey - it works!
But all the same it's strange that \n does not work - according to my books about PHP it aught to!

BR
Lars
User avatar
notuo
Posts: 258
Joined: Sat Jul 09, 2005 8:07 pm
Location: Tlalpan, Mexico City, Mexico

Post by notuo »

Hi

\n is a newline but just for the browser. Serves to format your generated code (ie. View source)

<br> is to really break line in the final display

I defined a NL= "<br>\n" to have both at the same time

Regards
User avatar
MikeyB
Top Contributor
Posts: 511
Joined: Fri Jun 09, 2006 10:38 am
Location: UK
Contact:

Post by MikeyB »

When you echo \n in PHP it does create a newline.

If the echo was going to a text file for example, you would see it no problem.

When the PHP is outputting to a browser the newline is still there, and can be seen if you view source, but it will show as a space on the page in your browser as newlines are not rendered in HTML, so you have to use the html code for a newline which is <br>

As a quick example create an HTML file with this code:

Code: Select all

<html>
<body>
1
2
3
</body>
</html>
Now even though you have a newline between the 1, 2 & 3 in the code, when viewed in your browser you will see 1 2 3 all on one line with spaces in between.

To get each number onto a new line you need to do:

Code: Select all

<html>
<body>
1<br>
2<br>
3<br>
</body>
</html>
or even

Code: Select all

<html>
<body>
1<br>2<br>3<br>
</body>
</html>
larshgf
Posts: 8
Joined: Fri Nov 03, 2006 12:29 pm
Location: Denmark

Post by larshgf »

Thanks for good answers!
BTW - in which way can I see the PHP generated HTML source in rapid PHP?
User avatar
jerm
Posts: 47
Joined: Wed Sep 06, 2006 8:06 am
Location: East Sussex UK
Contact:

Post by jerm »

Try using \r\n (carriage return plus newline). It works for me.
User avatar
MikeyB
Top Contributor
Posts: 511
Joined: Fri Jun 09, 2006 10:38 am
Location: UK
Contact:

Post by MikeyB »

larshgf wrote:Thanks for good answers!
BTW - in which way can I see the PHP generated HTML source in rapid PHP?
RapidPHP has it's own built in webserver, but you'll need to install PHP:
http://www.forums.blumentals.net/viewtopic.php?t=2384


jerm wrote:Try using \r\n (carriage return plus newline). It works for me.
That will still show as a space in the rendered HTML rather than an actual line break.

The above sample code I posted I tested on our Windows server which uses \r\n for and will still show a space in the browser.
Post Reply