"\n" in PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mudge

    "\n" in PHP

    I am reading a book called "PHP, Apache, MySQL Web Development"


    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=\"scrolle r\">\n";
    while ($row = mysql_fetch_arr ay($result))
    {
    echo "<span class='commentn ame'>".
    htmlspecialchar s($row['name']).
    "</span><span class='commentd ate'>
    (". date("1 F j, Y H:i", strtotime($row['comment_date'])).
    ")</span>\n";

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



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

    #2
    Re: &quot;\n&quo t; in PHP

    On Sun, 11 Jul 2004 17:48:44 GMT, Mudge <mark1822@hotma il.com> wrote:
    [color=blue]
    >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?[/color]

    What makes you think PHP will ignore the "\n"s? It does not.
    [color=blue]
    >For example:
    >
    >if (mysql_num_rows ($result))
    >{
    > echo "<div class=\"scrolle r\">\n";
    >
    >Why does the book put the "\n"s in there?[/color]

    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 <andy@andyh.co. uk> / Space: disk usage analysis tool
    http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

    Comment

    • Lc

      #3
      Re: &quot;\n&quo t; in PHP

      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" <mark1822@hotma il.com> a écrit dans le message de
      news:%3fIc.6963 7$Oq2.39135@att bi_s52...[color=blue]
      > 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=\"scrolle r\">\n";
      > while ($row = mysql_fetch_arr ay($result))
      > {
      > echo "<span class='commentn ame'>".
      > htmlspecialchar s($row['name']).
      > "</span><span class='commentd ate'>
      > (". date("1 F j, Y H:i", strtotime($row['comment_date'])).
      > ")</span>\n";
      >
      > echo "<p class='commentt ext'>\n".
      > nl2br(htmlspeci alchars($row['comment'])).
      > "\n</p>\n";
      > }
      > echo "</div>\n";
      > }
      > echo "<br />\n";
      >
      >
      >
      > Why does the book put the "\n"s in there?
      >[/color]


      Comment

      • Lc

        #4
        Re: &quot;\n&quo t; in PHP

        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" <mark1822@hotma il.com> a écrit dans le message de
        news:%3fIc.6963 7$Oq2.39135@att bi_s52...[color=blue]
        > 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=\"scrolle r\">\n";
        > while ($row = mysql_fetch_arr ay($result))
        > {
        > echo "<span class='commentn ame'>".
        > htmlspecialchar s($row['name']).
        > "</span><span class='commentd ate'>
        > (". date("1 F j, Y H:i", strtotime($row['comment_date'])).
        > ")</span>\n";
        >
        > echo "<p class='commentt ext'>\n".
        > nl2br(htmlspeci alchars($row['comment'])).
        > "\n</p>\n";
        > }
        > echo "</div>\n";
        > }
        > echo "<br />\n";
        >
        >
        >
        > Why does the book put the "\n"s in there?
        >[/color]



        Comment

        • Gary L. Burnore

          #5
          Re: &quot;\n&quo t; in PHP

          On Sun, 11 Jul 2004 20:39:52 +0200, "Lc" <hoho@not.me> wrote:
          [color=blue]
          >Quite easy to understand:[/color]
          Once was enough, top poster.


          --
          gburnore@databa six 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
          =============== =============== =============== =============== ===============

          Comment

          • Michael Austin

            #6
            Re: &quot;\n&quo t; in PHP

            Andy Hassall wrote:
            [color=blue]
            > On Sun, 11 Jul 2004 17:48:44 GMT, Mudge <mark1822@hotma il.com> wrote:
            >
            >[color=green]
            >>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?[/color]
            >
            >
            > What makes you think PHP will ignore the "\n"s? It does not.
            >
            >[color=green]
            >>For example:
            >>
            >>if (mysql_num_rows ($result))
            >>{
            >> echo "<div class=\"scrolle r\">\n";
            >>
            >>Why does the book put the "\n"s in there?[/color]
            >
            >
            > 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.[/color]

            unless of course you are using pre /pre tags then it will render the
            data on the next line...
            [color=blue]
            >
            > --
            > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
            > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space[/color]

            Michael Austin.

            Comment

            • Lc

              #7
              Re: &quot;\n&quo t; in PHP

              Michael, could you give us an example of "pre pre tag"? tx,

              "Michael Austin" <maustin@firstd basource.com> a écrit dans le message de
              news:6djIc.1461 6$v73.1473@news svr24.news.prod igy.com...[color=blue]
              > Andy Hassall wrote:
              >[color=green]
              > > On Sun, 11 Jul 2004 17:48:44 GMT, Mudge <mark1822@hotma il.com> wrote:
              > >
              > >[color=darkred]
              > >>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[/color][/color][/color]
              driving[color=blue][color=green][color=darkred]
              > >>me crazy because php will ignore the "\n"s so i have no idea why this[/color][/color][/color]
              book[color=blue][color=green][color=darkred]
              > >>has them in there. Does anybody know?[/color]
              > >
              > >
              > > What makes you think PHP will ignore the "\n"s? It does not.
              > >
              > >[color=darkred]
              > >>For example:
              > >>
              > >>if (mysql_num_rows ($result))
              > >>{
              > >> echo "<div class=\"scrolle r\">\n";
              > >>
              > >>Why does the book put the "\n"s in there?[/color]
              > >
              > >
              > > To make the produced HTML source easier to read, most likely.
              > >
              > > Although if they were concerned about readability they'd consider using[/color][/color]
              single[color=blue][color=green]
              > > quotes instead of doubles, and avoiding all those backslashes.
              > >
              > > Maybe you're thinking of the fact that newlines aren't significant in[/color][/color]
              HTML,[color=blue][color=green]
              > > i.e. it won't force a new line in the rendered output.[/color]
              >
              > unless of course you are using pre /pre tags then it will render the
              > data on the next line...
              >[color=green]
              > >
              > > --
              > > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
              > > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space[/color]
              >
              > Michael Austin.[/color]


              Comment

              • bonehead

                #8
                Re: &quot;\n&quo t; in PHP

                Mudge wrote:
                [color=blue]
                > Why does the book put the "\n"s in there?[/color]

                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:


                Comment

                • Mudge

                  #9
                  Re: &quot;\n&quo t; in PHP

                  bonehead wrote:
                  [color=blue]
                  > Mudge wrote:
                  >[color=green]
                  >> Why does the book put the "\n"s in there?[/color]
                  >
                  > 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[/color]

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

                  Comment

                  • Michael Austin

                    #10
                    Re: &quot;\n&quo t; in PHP

                    Lc wrote:
                    [color=blue]
                    > Michael, could you give us an example of "pre /pre tag"? tx,
                    >[/color]
                    <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.

                    Comment

                    • marathon

                      #11
                      Re: &quot;\n&quo t; in PHP

                      On Mon, 12 Jul 2004 05:13:49 GMT, Mudge in comp.lang.php wrote:[color=blue]
                      >bonehead wrote:[/color]

                      [color=blue][color=green]
                      >> 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[/color][/color]
                      [color=blue]
                      >Thanks bonehead. Do you by any chance recommend any other books on PHP or
                      >CSS or XML?[/color]

                      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.

                      Comment

                      • Jeffrey Silverman

                        #12
                        Re: &quot;\n&quo t; in PHP

                        On Mon, 12 Jul 2004 05:13:49 +0000, Mudge wrote:
                        [color=blue]
                        > 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.[/color]

                        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.
                        [color=blue]
                        > Thanks bonehead. Do you by any chance recommend any other books on PHP
                        > or CSS or XML?[/color]

                        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 | jeffrey@pantsjh u.edu **
                        Website | http://www.newtnotes.com

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

                        Comment

                        • Jeffrey Silverman

                          #13
                          Re: &quot;\n&quo t; in PHP

                          On Sun, 11 Jul 2004 19:09:57 +0100, Andy Hassall wrote:
                          [color=blue]
                          > 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.[/color]

                          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 | jeffrey@pantsjh u.edu **
                          Website | http://www.newtnotes.com

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

                          Comment

                          • Andy Hassall

                            #14
                            Re: &quot;\n&quo t; in PHP

                            On Mon, 12 Jul 2004 17:22:59 -0400, Jeffrey Silverman <jeffrey@pantsj hu.edu>
                            wrote:
                            [color=blue]
                            >On Sun, 11 Jul 2004 19:09:57 +0100, Andy Hassall wrote:
                            >[color=green]
                            >> 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.[/color]
                            >
                            >Using single quotes, while making the PHP a bit more readable because of
                            >the lack of backslashes, will prevent the newlines from being printed![/color]

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

                            echo "<div class=\"scrolle r\">\n";

                            vs.

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

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

                            Comment

                            • bonehead

                              #15
                              Re: &quot;\n&quo t; in PHP

                              Jeffrey Silverman wrote:[color=blue]
                              > 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.[/color]

                              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.
                              [color=blue]
                              > I'm not bonehead, either. I have, however, found that "PHP and MySQL Web
                              > Development, Second Edition" by Welling/Thompson is very nice.[/color]

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

                              Comment

                              Working...