Newline

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kruxz
Forum Newbie
Posts: 2
Joined: Thu Jan 29, 2009 9:49 pm

Newline

Post by kruxz »

Hi,

I'm new to php. I downloaded and installed the following:

1. php-5.2.8-win32 (zip file--I followed the manual installation steps from the manual)
2. apache httpd server 2.2.11.

I tested the installation using

Code: Select all

<?php
     phpinfo();
?>
It worked.

My problem is it cannot recognize a newline character (\n). For example,

Code: Select all

<?php
     echo "Hello \n";
     echo "World";
?>
The output is like this:
Hello World

It should be like this:
Hello
World

I tried using

Code: Select all

<?php
     echo "Hello" . "<br>";
     echo "World";
?>
It worked. But I am just confused why it doesn't work using the \n. Does it have something to do with the configuration of php/apache? Hope someone can shed a light on me.

Thanks.

--Ric :?
Last edited by pickle on Fri Jan 30, 2009 9:56 am, edited 1 time in total.
Reason: Added proper [code=php][/code] tags
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Newline

Post by Benjamin »

Please post in the appropriate forum and use [ code ] tags when posting code.
User avatar
nor0101
Forum Commoner
Posts: 53
Joined: Thu Jan 15, 2009 12:06 pm
Location: Wisconsin

Re: Newline

Post by nor0101 »

It's because of the way HTML is rendered in a browser. The <br /> tag creates a new line in the rendered HTML. However, if you view the source of the \n version you'll see that the newline was indeed handled correctly. Alternately, you can wrap your output in <pre></pre>.

Cheers,
kruxz
Forum Newbie
Posts: 2
Joined: Thu Jan 29, 2009 9:49 pm

Re: Newline

Post by kruxz »

astions: Sorry. I'm new to this forum...doesn't know which way to go around.

nor0101: Thank you. Now I understand. I viewed the source, and indeed, just like you said the newline was handled correctly. So does this mean that it's better to use the <br /> than \n? So why use \n if it doesn't work on major browsers?
User avatar
nor0101
Forum Commoner
Posts: 53
Joined: Thu Jan 15, 2009 12:06 pm
Location: Wisconsin

Re: Newline

Post by nor0101 »

It's nice to be able to output a newline character in LOTS of situations. At least now when you come across it you'll know how. Hint: PHP can do more than generate HTML.
Post Reply