473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

"\n" in PHP

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
16 5884
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
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
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
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
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
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
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
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
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
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
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
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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
1
by: David Furey | last post by:
Hi I have an XML documnet and a XSLT document as shown below THe XSLT document brings back a filtered docmument that has the VendorName that starts with a particular sub-string This works as...
2
by: Eric Osman | last post by:
Hi, I'm looking for a javascript function that will convert input such as this: <CLUB Code=" into this: &lt;CLUB Code=&quot;
3
by: NecroJoe | last post by:
I am using PHP to generate a little javascript for one of my pages. In short it allows a user to select a value from a list and pop it into a form field on a seperate page. This works well unless...
5
by: Mateusz Loskot | last post by:
Hi, I'd like to ask how XML parsers should handle attributes which consists of &quot; entity as value. I know XML allows to use both: single and double quotes as attribute value terminator. That's...
3
by: Arpi Jakab | last post by:
I have a main project that depends on projects A and B. The main project's additional include directories list is: ...\ProjectA\Dist\Include ...\ProjectB\Dist\Include Each of the include...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
8
by: Ulysse | last post by:
Hello, I need to clean the string like this : string = """ bonne mentalit&eacute; mec!:) \n <br>bon pour info moi je suis un serial posteur arceleur dictateur ^^* \n ...
1
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
4
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.