PHP question - line break?
Posted by: Cider (---.msl-mt.client.bresnan.net)
Date: December 24, 2007 12:33PM

(apologies in advance if this is an inappropriate forum for this question)

I'm brand new to PHP (just wrote my 'hello world' in PHP tonight), and using the O'Reilly book, Learning PHP 5. I installed WampServer for this purpose, and it seems to be working fine, as far as I can tell.

Anyway, I understand that to create a line break, the command

print "\n";

is used. However, it isn't working for me! I've checked in both Firefox & Opera, (using Notepad++ for editing) and neither one breaks the lines. Searching online has convinced me that this is indeed the correct command.

For example:

print "apples\noranges\npears\npineapples";

returns:

apples oranges pears pineapples

instead of what I would have expected, which should be:

apples
oranges
pears
pineapples

...shouldn't it? Clearly I'm missing something (and can't yet rule out my brain)! Any suggestions?

Options: ReplyQuote
Re: PHP question - line break?
Posted by: toivo (---.nsw.bigpond.net.au)
Date: December 24, 2007 01:22PM

Hi,

Your example works all right in a PHP command line interface (CLI) script. If you use a browser to run your script, it should first create a HTML/XHTML document with the header and body tags and then print something like this:

print "<p>apples<br />oranges<br />pears<br />pineapples</p>";

Regards,

toivo
Sydney, Australia

Options: ReplyQuote
Re: PHP question - line break?
Posted by: clorne (---.dynamic.dsl.as9105.com)
Date: December 24, 2007 01:41PM

or

/n is newline not line break, if you want to add a line break you can use

the php string function — nl2br — which inserts HTML line breaks before all newlines in a string

so with your example -

print nl2br("apples\noranges\npears\npineapples"winking smiley;

Options: ReplyQuote
Re: PHP question - line break? [Solved]
Posted by: Cider (---.msl-mt.client.bresnan.net)
Date: December 28, 2007 12:51AM

Ah ok, thanks guys!

I understand a bit better now, the book isn't quite clear about how it works, which is mildly frustrating as it's billed on the cover as, "A pain-free introduction to building interactive web sites," yet doesn't clearly explain how things such as newline/line break actually are used & displayed. Nowhere in the book does it point out that 'nl2br' is needed in a situation like that.

Thanks again!

Options: ReplyQuote


Sorry, only registered users may post in this forum.