tags:

views:

91

answers:

5

I am newbie to PHP I have written the following program:

$address=array('[email protected]','[email protected]','[email protected]');

foreach($address as $value)
{
     echo "processing $value\n";
}

If you see I have \n in the echo statement but I am not getting the output on new line.

How can I get each output on a new line?

+7  A: 

If you are outputting this as HTML then you must of course use a HTML break <br />.

Ciarán Walsh
+4  A: 

If you're working in a browser, you need to break lines with

<br>
Pekka
got it thanks its working can you please elaborate on it why is that so ? and how come \n isnt working?
Josh
if you were to view the source from the browser you would see new lines in the source, \n will only work as newline in a browser if it is inside <pre></pre> tags, otherwise the html way of doing a newline is <br />
Tim
Browsers ignore text formatting (line breaks, breaking spaces, etc) in HTML to allow for the markup to be formatted to be readable to humans (otherwise, giant paragraphs would have to be on one line, and all sorts of other messes). You can view blocks of text preformatted by wrapping them in the `<pre> ... </pre>` tags, or setting the CSS of an element to `white-space: pre;`
Dereleased
Actually I think it's `<br/>`
aib
<br/> is valid XML if your're not using an XHTML doctype <br> is fine
Alexandre Jasmin
That depends on the doctype. In 4.01, the use of self closing tags is not valid AFAIK.
Pekka
+2  A: 

You need to print an HTML line break instead:

<br/>

Since you are printing to a browser

bkildow
but we are working in PHP right? so cant PHP direct the browser to leave a line? am sorry seems like i am asking very basic stuff ;)
Josh
A: 

You may want to wrap your output in a <pre> tag as your browser is expecting HTML and is just collapsing the whitespace. The pre tag will reflect the whitespace (\t \n etc);

Alternately you can use a break tag, or wrap the data in a block display element. (eg: <p> or <div>)

sparkey0
Remember, SO accepts some HTML as markup for formatting, always wrap it with backticks (`).
Dereleased
Thanks! Will keep in mind in future posts.
sparkey0
A: 

\n will line break properly when you view the source, but not in the HTML display. As mentioned, you need to use the <br/> node for HTML

zaphod