By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

"\n" in PHP

P: n/a
I am reading a book called "PHP, Apache, MySQL Web Development"
http://www.wrox.com/WileyCDA/WroxTit...764557440.html

and in its example code it sometimes will have a "\n" in it. It is driving
me crazy because php will ignore the "\n"s so i have no idea why this book
has them in there. Does anybody know?

For example:
if (mysql_num_rows($result))
{
echo "<div class=\"scroller\">\n";
while ($row = mysql_fetch_array($result))
{
echo "<span class='commentname'>".
htmlspecialchars($row['name']).
"</span><span class='commentdate'>
(". date("1 F j, Y H:i", strtotime($row['comment_date'])).
")</span>\n";

echo "<p class='commenttext'>\n".
nl2br(htmlspecialchars($row['comment'])).
"\n</p>\n";
}
echo "</div>\n";
}
echo "<br />\n";

Why does the book put the "\n"s in there?
Jul 17 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On Sun, 11 Jul 2004 17:48:44 GMT, Mudge <ma******@hotmail.com> wrote:
I am reading a book called "PHP, Apache, MySQL Web Development"
http://www.wrox.com/WileyCDA/WroxTit...764557440.html

and in its example code it sometimes will have a "\n" in it. It is driving
me crazy because php will ignore the "\n"s so i have no idea why this book
has them in there. Does anybody know?
What makes you think PHP will ignore the "\n"s? It does not.
For example:

if (mysql_num_rows($result))
{
echo "<div class=\"scroller\">\n";

Why does the book put the "\n"s in there?


To make the produced HTML source easier to read, most likely.

Although if they were concerned about readability they'd consider using single
quotes instead of doubles, and avoiding all those backslashes.

Maybe you're thinking of the fact that newlines aren't significant in HTML,
i.e. it won't force a new line in the rendered output. Nothing to do with PHP,
though.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2

P: n/a
Lc
Quite easy to understand:

look at the code generated by your web server after php code has been
interpreted (view source in your IExplorer menu).

When you use /n, the code continues on a new line.
If you don't use /n, the code continues on the same line.

In your example /n is used to facilitate the reading of the HTML code
generated by the web server.

If you use SQL database, /n will be usefull to introduce "new lines" in a
single text stored in only one field of your database...
"Mudge" <ma******@hotmail.com> a crit dans le message de
news:%3fIc.69637$Oq2.39135@attbi_s52...
I am reading a book called "PHP, Apache, MySQL Web Development"
http://www.wrox.com/WileyCDA/WroxTit...764557440.html

and in its example code it sometimes will have a "\n" in it. It is driving
me crazy because php will ignore the "\n"s so i have no idea why this book
has them in there. Does anybody know?

For example:
if (mysql_num_rows($result))
{
echo "<div class=\"scroller\">\n";
while ($row = mysql_fetch_array($result))
{
echo "<span class='commentname'>".
htmlspecialchars($row['name']).
"</span><span class='commentdate'>
(". date("1 F j, Y H:i", strtotime($row['comment_date'])).
")</span>\n";

echo "<p class='commenttext'>\n".
nl2br(htmlspecialchars($row['comment'])).
"\n</p>\n";
}
echo "</div>\n";
}
echo "<br />\n";

Why does the book put the "\n"s in there?

Jul 17 '05 #3

P: n/a
Lc
Quite easy to understand:

look at the code generated by your web server after php code has been
interpreted (view source in your IExplorer menu).

When you use /n, the code continues on a new line.
If you don't use /n, the code continues on the same line.

In your example /n is used to facilitate the reading of the HTML code
generated by the web server.

If you use SQL database, /n will be usefull to introduce "new lines" in a
single text stored in only one field of your database...
"Mudge" <ma******@hotmail.com> a crit dans le message de
news:%3fIc.69637$Oq2.39135@attbi_s52...
I am reading a book called "PHP, Apache, MySQL Web Development"
http://www.wrox.com/WileyCDA/WroxTit...764557440.html

and in its example code it sometimes will have a "\n" in it. It is driving
me crazy because php will ignore the "\n"s so i have no idea why this book
has them in there. Does anybody know?

For example:
if (mysql_num_rows($result))
{
echo "<div class=\"scroller\">\n";
while ($row = mysql_fetch_array($result))
{
echo "<span class='commentname'>".
htmlspecialchars($row['name']).
"</span><span class='commentdate'>
(". date("1 F j, Y H:i", strtotime($row['comment_date'])).
")</span>\n";

echo "<p class='commenttext'>\n".
nl2br(htmlspecialchars($row['comment'])).
"\n</p>\n";
}
echo "</div>\n";
}
echo "<br />\n";

Why does the book put the "\n"s in there?


Jul 17 '05 #4

P: n/a
On Sun, 11 Jul 2004 20:39:52 +0200, "Lc" <ho**@not.me> wrote:
Quite easy to understand:

Once was enough, top poster.
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
DataBasix | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ۳
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Jul 17 '05 #5

P: n/a
Andy Hassall wrote:
On Sun, 11 Jul 2004 17:48:44 GMT, Mudge <ma******@hotmail.com> wrote:

I am reading a book called "PHP, Apache, MySQL Web Development"
http://www.wrox.com/WileyCDA/WroxTit...764557440.html

and in its example code it sometimes will have a "\n" in it. It is driving
me crazy because php will ignore the "\n"s so i have no idea why this book
has them in there. Does anybody know?

What makes you think PHP will ignore the "\n"s? It does not.

For example:

if (mysql_num_rows($result))
{
echo "<div class=\"scroller\">\n";

Why does the book put the "\n"s in there?

To make the produced HTML source easier to read, most likely.

Although if they were concerned about readability they'd consider using single
quotes instead of doubles, and avoiding all those backslashes.

Maybe you're thinking of the fact that newlines aren't significant in HTML,
i.e. it won't force a new line in the rendered output.


unless of course you are using pre /pre tags then it will render the
data on the next line...

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space


Michael Austin.
Jul 17 '05 #6

P: n/a
Lc
Michael, could you give us an example of "pre pre tag"? tx,

"Michael Austin" <ma*****@firstdbasource.com> a crit dans le message de
news:6d******************@newssvr24.news.prodigy.c om...
Andy Hassall wrote:
On Sun, 11 Jul 2004 17:48:44 GMT, Mudge <ma******@hotmail.com> wrote:

I am reading a book called "PHP, Apache, MySQL Web Development"
http://www.wrox.com/WileyCDA/WroxTit...764557440.html

and in its example code it sometimes will have a "\n" in it. It is drivingme crazy because php will ignore the "\n"s so i have no idea why this bookhas them in there. Does anybody know?

What makes you think PHP will ignore the "\n"s? It does not.

For example:

if (mysql_num_rows($result))
{
echo "<div class=\"scroller\">\n";

Why does the book put the "\n"s in there?

To make the produced HTML source easier to read, most likely.

Although if they were concerned about readability they'd consider using single quotes instead of doubles, and avoiding all those backslashes.

Maybe you're thinking of the fact that newlines aren't significant in HTML, i.e. it won't force a new line in the rendered output.


unless of course you are using pre /pre tags then it will render the
data on the next line...

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space


Michael Austin.

Jul 17 '05 #7

P: n/a
Mudge wrote:
Why does the book put the "\n"s in there?


I happen to be working through this book right now also. I've noticed,
at least in the early chapters, that it's basically just a matter of
style. The newline characters are not always necessary, but sometimes
they do make the html output a little more readable as other posters
have already pointed out. If you're curious, write one of the example
scripts, get it to run, and then go back and remove the newline
characters and see the difference.

Personally the chapter I found most useful was Chapter 11. The examples
of session logins and cookie setting are very useful.

BTW, as with many Wrox books, this one appears to have lots of genuine
errors, many of which are just editorial typos but which nevertheless
affect whether the code will run or not. They really need to post an
errata page for this book, but in the meanwhile, take a look through the
P2P Forum for this book just to get an idea, and keep the link
bookmarked because you'll need it:

http://p2p.wrox.com/forum.asp?FORUM_ID=120
Jul 17 '05 #8

P: n/a
bonehead wrote:
Mudge wrote:
Why does the book put the "\n"s in there?


I happen to be working through this book right now also. I've noticed,
at least in the early chapters, that it's basically just a matter of
style. The newline characters are not always necessary, but sometimes
they do make the html output a little more readable as other posters
have already pointed out. If you're curious, write one of the example
scripts, get it to run, and then go back and remove the newline
characters and see the difference.

Personally the chapter I found most useful was Chapter 11. The examples
of session logins and cookie setting are very useful.

BTW, as with many Wrox books, this one appears to have lots of genuine
errors, many of which are just editorial typos but which nevertheless
affect whether the code will run or not. They really need to post an
errata page for this book, but in the meanwhile, take a look through the
P2P Forum for this book just to get an idea, and keep the link
bookmarked because you'll need it:

http://p2p.wrox.com/forum.asp?FORUM_ID=120


Thanks bonehead. Do you by any chance recommend any other books on PHP or
CSS or XML?
Jul 17 '05 #9

P: n/a
Lc wrote:
Michael, could you give us an example of "pre /pre tag"? tx,

<other stuff snipped>

html body
<?php
echo "this is a test \n";
echo "this is a test<br> \n";
echo "this is a test \n";
echo "<pre>";
echo "this is a test \n";
echo "this is a test<br> \n";
echo "this is a test \n";
echo "</pre>";
echo "this is a test \n";
echo "this is a test<br> \n";
echo "this is a test \n";

?>
</body></html>

produces this page source html body removed so your reader doesn't try
to interpret it...
==================================================
this is a test
this is a test<br>
this is a test
<pre>this is a test
this is a test<br>
this is a test
</pre>this is a test
this is a test <br>
this is a test
==================================================

produces on the screen:
==================================================
this is a test this is a test
this is a test

this is a test
this is a test

this is a test

this is a test this is a test
this is a test
==================================================

Note that the <br> within the PRE[formatted text] field also generates a
line break.

Michael Austin.
Jul 17 '05 #10

P: n/a
On Mon, 12 Jul 2004 05:13:49 GMT, Mudge in comp.lang.php wrote:
bonehead wrote:
BTW, as with many Wrox books, this one appears to have lots of genuine
errors, many of which are just editorial typos but which nevertheless
affect whether the code will run or not. They really need to post an
errata page for this book, but in the meanwhile, take a look through the
P2P Forum for this book just to get an idea, and keep the link
bookmarked because you'll need it:

http://p2p.wrox.com/forum.asp?FORUM_ID=120

Thanks bonehead. Do you by any chance recommend any other books on PHP or
CSS or XML?


I'm not bonehead, but one very good beginners PHP book (IMO) is;

Larry Ullman's book "PHP For The World Wide Web" Second Edition. It's a
visual quick start, type of book. He has a good teaching style -- at
least for moi.

In terms of CSS, anything done by Eric Meyers is generally considered
top drawer from what I've heard. Dunno about XML though.

--
S.Allen
-----------------------------------------------
barnyard Monday Jul 12 2004 11:25:01 AM EDT
-----------------------------------------------
You may be recognized soon. Hide.
Jul 17 '05 #11

P: n/a
On Mon, 12 Jul 2004 05:13:49 +0000, Mudge wrote:
I happen to be working through this book right now also. I've noticed,
at least in the early chapters, that it's basically just a matter of
style. The newline characters are not always necessary, but sometimes
they do make the html output a little more readable as other posters
have already pointed out. If you're curious, write one of the example
scripts, get it to run, and then go back and remove the newline
characters and see the difference.
I definitely think the newlines are important. Try writing 1200 lines of
PHP that output HTML *without* newlines and then go back and debug your
output! You will greatly appreciate the additioanl formatting, even if the
actual end-user never sees it in her browser.
Thanks bonehead. Do you by any chance recommend any other books on PHP
or CSS or XML?


I'm not bonehead, either. I have, however, found that "PHP and MySQL Web
Development, Second Edition" by Welling/Thompson is very nice.

--
Jeffrey D. Silverman | je*****@pantsjhu.edu **
Website | http://www.newtnotes.com

(** Drop "pants" to reply by email)

Jul 17 '05 #12

P: n/a
On Sun, 11 Jul 2004 19:09:57 +0100, Andy Hassall wrote:
To make the produced HTML source easier to read, most likely.

Although if they were concerned about readability they'd consider using
single
quotes instead of doubles, and avoiding all those backslashes.

Maybe you're thinking of the fact that newlines aren't significant in
HTML,
i.e. it won't force a new line in the rendered output. Nothing to do with
PHP, though.


Using single quotes, while making the PHP a bit more readable because of
the lack of backslashes, will prevent the newlines from being printed!

<?
echo "hello\n";
?>

Prints hello plus newline.

<?
echo 'hello\n';
?>

prints hello plus the literal backslash-n characters!

--
Jeffrey D. Silverman | je*****@pantsjhu.edu **
Website | http://www.newtnotes.com

(** Drop "pants" to reply by email)

Jul 17 '05 #13

P: n/a
On Mon, 12 Jul 2004 17:22:59 -0400, Jeffrey Silverman <je*****@pantsjhu.edu>
wrote:
On Sun, 11 Jul 2004 19:09:57 +0100, Andy Hassall wrote:
To make the produced HTML source easier to read, most likely.

Although if they were concerned about readability they'd consider using
single
quotes instead of doubles, and avoiding all those backslashes.


Using single quotes, while making the PHP a bit more readable because of
the lack of backslashes, will prevent the newlines from being printed!


I was referring to using single quotes for the HTML attributes:

echo "<div class=\"scroller\">\n";

vs.

echo "<div class='scroller'>\n";

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #14

P: n/a
Jeffrey Silverman wrote:
I definitely think the newlines are important. Try writing 1200 lines of
PHP that output HTML *without* newlines and then go back and debug your
output! You will greatly appreciate the additioanl formatting, even if the
actual end-user never sees it in her browser.
I agree, there are times when it definitely helps. The book being
discussed in this thread actually has some other real good generic
examples of debugging.
I'm not bonehead, either. I have, however, found that "PHP and MySQL Web
Development, Second Edition" by Welling/Thompson is very nice.


I agree with this also, the Luke and Laura book is an excellent
reference, I use it often. The SAMS books (not necessarily the "Teach
Yourself" series, but the more substantial ones like this one) are
usually more reliable technically. But the Wrox books are still useful
as primers because of the "1,2,3" tutorial approach, in spite of the
flaws. Personally whenever I'm learning anything for the first time I
really like to be totally led by the nose...just ask my girlfriend...
Jul 17 '05 #15

P: n/a
> I'm not bonehead, either. I have, however, found that "PHP and MySQL Web
Development, Second Edition" by Welling/Thompson is very nice.


I second this. Very informative book and comes with the code talked about in
the book on CD so you can mess with it. Also, check out the PHP Cookbook
from O'Reilly publishing.

PHP and MySQL Web Development, Second Edition by Luke Welling, Laura Thomson
(http://www.amazon.com/exec/obidos/tg...17428?v=glance)

PHP Cookbook by David Sklar, Adam Trachtenberg
(http://www.oreilly.com/catalog/phpckbk/)

-William
Jul 17 '05 #16

P: n/a
On Mon, 12 Jul 2004 22:29:41 +0100, Andy Hassall wrote:
was referring to using single quotes for the HTML attributes:

echo "<div class=\"scroller\">\n";

vs.

echo "<div class='scroller'>\n";


Yes. I wholeheartedly agree with you there.

As a matter of fact, I recommend single quotes for use around
associative array keys:

$array['key']

vs.

$array["key"]

In fact, I use single quotes around everything unless the string is going
to be printed directly in the browser.

I guess it is a matter of style in most cases but the PHP newbie (and
shell or Perl scripting newbiem for that matter) may not realize the big
differences between single and double quotes. (Oh, and the SQL newbie,
although the meaning of single vs double quotes is a bit different in SQL
than in bash, Perl, or PHP).

hmm.... that was a bit of a tangent! OR was it? In any case, if you are a
newbie (not you, Hassal), and you are reading this post, Google on the
differences between single and double quotes. No sense in restating that
here. Except to point out that there *are* differences, and
newbies may not even realize that at all.

allright, enough rambling. It is just a wee bit after 7AM my time as I
write this and there is no sense in going back to bed but of course that
is also why my post may be a bit disjointed, especially on account of the
fact that I've actually been up since 5:30...hmmm...more rambling.
See y'alls later!

--
Jeffrey Silverman
je*****@pantsjhu.edu
Drop "pants" to reply by email

Jul 17 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.