Page 1 of 1

Newline

Posted: Thu Jan 29, 2009 11:06 pm
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 :?

Re: Newline

Posted: Thu Jan 29, 2009 11:09 pm
by Benjamin
Please post in the appropriate forum and use [ code ] tags when posting code.

Re: Newline

Posted: Thu Jan 29, 2009 11:10 pm
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,

Re: Newline

Posted: Thu Jan 29, 2009 11:44 pm
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?

Re: Newline

Posted: Fri Jan 30, 2009 9:17 am
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.