0

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?

1
  • I don't see any code; I see only the closing PHP tag, nothing else. Commented Dec 22, 2009 at 19:39

5 Answers 5

7

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

<br>

6 Comments

got it thanks its working can you please elaborate on it why is that so ? and how come \n isnt working?
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;
Actually I think it's <br/>
<br/> is valid XML if your're not using an XHTML doctype <br> is fine
That depends on the doctype. In 4.01, the use of self closing tags is not valid AFAIK.
|
2

You need to print an HTML line break instead:

<br/>

Since you are printing to a browser

1 Comment

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 ;)
0

\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

Comments

0

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>)

1 Comment

Remember, SO accepts some HTML as markup for formatting, always wrap it with backticks (`).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.