Question about embedded newlines

1 view
Skip to first unread message

kristin robinson

unread,
Feb 5, 2011, 7:29:46 PM2/5/11
to Introduction to PHP, Jan-Apr 2011, Matt's Section
So Evolve and I were looking at code samples taken from the php online
manual. Here is one example

echo 'You can also have embedded newlines in
strings this way as it is
okay to do';

(from page: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single)

We were surprised when looking at involve's code exercise (http://
codepad.org/udcdHD39) that the Le Guin poem wouldn't render in the
browser with the embedded newlines. It just runs all together unless
breaks or returns are manually added. This holds true for single,
double, heredoc syntax and nowdoc syntax. So where are the embedded
newlines?

I looked at the source code and the source code had preserved the
newlines. But since there was no HTML or other instruction to tell
the browser to preserve the formatting, the newlines are only
preserved in the source code the PHP is creating.

Matt: is this understanding correct?

Matt Buscemi

unread,
Feb 5, 2011, 8:18:19 PM2/5/11
to intro2php-m...@googlegroups.com
Kristin,

Yes, this is correct.  PHP will output any newlines that you write into a string, even if you actually hit return and continue typing on the next line.  Codepad will even show you the newlines in the output, as in the example of your Ursula Le Guin quote.  However, the HTML produced by running the PHP file is ultimately subjected to the browser, which will ignore newlines and only place them where it finds the <br/> tag, or after block elements.

Hope this helps!

- Matt

Jes Gagnon

unread,
Feb 8, 2011, 2:37:28 PM2/8/11
to intro2php-m...@googlegroups.com
Hey Matt,

Sorry I didn't make it to the last meeting! I should probably drop out of the course, sadly. Turns out I moved on the first, unexpectedly, into a house that is now being reno'd. So i'm not even living in it yet, and thus everything is a mess. Good luck to everyone else!

Thanks,

Jes Gagnon
Reply all
Reply to author
Forward
0 new messages