Newline

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

    #1

    Newline

    Hello Everyone,

    I am running PHP for Apache on a Windows XP machine.

    When I attempt to use \n in my code to generate a newline, it does not
    work. I've also see the other posts about using \r along with \n, but
    that doesn't work either. I'm sort of stuck here. Is there anything
    that I can do to generate a newline?

  • Carl Vondrick

    #2
    Re: Newline

    infernon wrote:[color=blue]
    > When I attempt to use \n in my code to generate a newline, it does not
    > work. I've also see the other posts about using \r along with \n, but
    > that doesn't work either. I'm sort of stuck here. Is there anything
    > that I can do to generate a newline?[/color]

    My bet is that you are doing this:

    echo 'Test\n';

    The problem here is the single quotes. The single quotes tell the parser to
    just spit out this string, and ignore any modifiers or variables.

    So, try this:

    echo 'Test'."\n";


    Good luck,
    Carl

    --
    Carl Vondrick
    Web-Engineer
    Carl Vondrick is a Professor of Computer Science at Columbia University, researching computer vision, machine learning, and AI applications.

    Comment

    • infernon

      #3
      Re: Newline

      Thanks for the quick response, Carl.

      I'm actually using double-quotes. The exact code is:


      printf("%1d %9s at \$%.2f each: \$%.2f\r\n"...


      What I've found is that the source actually contains a carriage return,
      but that the page doesn't render with one. I've tried this with both
      IE and FF to be sure that it wasn't a browser problem. The other side
      of it is that the command line puts the carriage return in as well.

      I apologize, I should have posted more information in my initial post.
      Thanks for your response.

      Comment

      • Carl Vondrick

        #4
        Re: Newline

        infernon wrote:[color=blue]
        > What I've found is that the source actually contains a carriage return,
        > but that the page doesn't render with one. I've tried this with both
        > IE and FF to be sure that it wasn't a browser problem. The other side
        > of it is that the command line puts the carriage return in as well.[/color]

        Ah! Your problem is with the HTML output, then, rather than PHP. HTML
        ignores white space. If you want to put a line return, you must put a
        break in: <br /> or <br> depending on the markup you are using (XHTML OR
        HTML).

        Alternatively, you can wrap your code in <pre> tags, which will print
        everything exactly as it is.

        Carl

        --
        Carl Vondrick
        Web-Engineer
        Carl Vondrick is a Professor of Computer Science at Columbia University, researching computer vision, machine learning, and AI applications.

        Comment

        • Jerry Stuckle

          #5
          Re: Newline

          infernon wrote:[color=blue]
          > Hello Everyone,
          >
          > I am running PHP for Apache on a Windows XP machine.
          >
          > When I attempt to use \n in my code to generate a newline, it does not
          > work. I've also see the other posts about using \r along with \n, but
          > that doesn't work either. I'm sort of stuck here. Is there anything
          > that I can do to generate a newline?
          >[/color]

          Look at your source - you'll see the newlines in there. But HTML ignores
          newline characters. You need to use an html <br>.

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • infernon

            #6
            Re: Newline

            OK, so I put the following line in and it worked:

            print "</br>";

            Is this correct practice?

            Comment

            • Carl Vondrick

              #7
              Re: Newline

              infernon wrote:[color=blue]
              > print "</br>";
              >
              > Is this correct practice?[/color]
              No, it needs to be either <br> or <br /> depending on what markup language
              you are using (HTML uses <br> and XHTML uses <br />).

              On the PHP side, the following is a little bit faster:

              echo '<br />';

              --
              Carl Vondrick
              Web-Engineer
              Carl Vondrick is a Professor of Computer Science at Columbia University, researching computer vision, machine learning, and AI applications.

              Comment

              • infernon

                #8
                Re: Newline

                Thank you, Carl. This was a big help!

                Comment

                • Jerry Stuckle

                  #9
                  Re: Newline

                  infernon wrote:[color=blue]
                  > OK, so I put the following line in and it worked:
                  >
                  > print "</br>";
                  >
                  > Is this correct practice?
                  >[/color]

                  Yep. Or, if your source is a string containing nl characters, use nl2br().

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Newline

                    Carl Vondrick wrote:[color=blue]
                    > infernon wrote:
                    >[color=green]
                    >>print "</br>";
                    >>
                    >>Is this correct practice?[/color]
                    >
                    > No, it needs to be either <br> or <br /> depending on what markup language
                    > you are using (HTML uses <br> and XHTML uses <br />).
                    >
                    > On the PHP side, the following is a little bit faster:
                    >
                    > echo '<br />';
                    >[/color]

                    But <br /> is invalid html 4.x, although browsers will generally process it OK.
                    It's only valid for xml or xhtml.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Carl Vondrick

                      #11
                      Re: Newline

                      Jerry Stuckle wrote:[color=blue]
                      > But <br /> is invalid html 4.x, although browsers will generally process
                      > it OK. It's only valid for xml or xhtml.[/color]
                      Yes, as I said above. :-)

                      --
                      Carl Vondrick
                      Web-Engineer
                      Carl Vondrick is a Professor of Computer Science at Columbia University, researching computer vision, machine learning, and AI applications.

                      Comment

                      • Andy Jeffries

                        #12
                        Re: Newline

                        On Thu, 13 Apr 2006 17:39:50 +0000, Carl Vondrick wrote:[color=blue][color=green]
                        >> print "</br>";
                        >>[/color]
                        > On the PHP side, the following is a little bit faster:
                        >
                        > echo '<br />';[/color]

                        While I don't dispute using echo instead of print and ' instead of " is
                        faster, has anyone actually done any benchmarks to see how much faster.

                        To be honest, I tend to use print (because I'm used to it) and "" (as I
                        generally would go back and add variables in later and may forget to
                        change the ' to ").

                        It's the same reason that in various coding standards it's recommended to
                        use {} even for single line ifs, so when the coder returns (or another one
                        edits the file) they're less likely to add a second indented line and not
                        see that the statement isn't wrapped in braces.

                        It's also faster to write functions inline, but at the expense of
                        readability/ease of maintenance. Not that we're comparing apples to
                        apples with that latter analogy (I know there's not much readability
                        difference in echo '' and print "").

                        Anyway, my point was - undeniably faster, but how much so... Anyone got
                        numbers?

                        Cheers,


                        Andy


                        --
                        Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                        http://www.gphpedit.org | PHP editor for Gnome 2
                        http://www.andyjeffries.co.uk | Personal site and photos

                        Comment

                        • Default User

                          #13
                          Re: Newline

                          Jerry Stuckle wrote:
                          [color=blue]
                          > infernon wrote:[/color]
                          [color=blue][color=green]
                          > > When I attempt to use \n in my code to generate a newline, it does
                          > > not work. I've also see the other posts about using \r along with
                          > > \n, but that doesn't work either. I'm sort of stuck here. Is
                          > > there anything that I can do to generate a newline?
                          > >[/color]
                          >
                          > Look at your source - you'll see the newlines in there. But HTML
                          > ignores newline characters. You need to use an html <br>.[/color]

                          It doesn't ignore it, rather treats it as whitespace.



                          Brian

                          Comment

                          • Carl Vondrick

                            #14
                            Re: Newline

                            Andy Jeffries wrote:[color=blue]
                            > Anyway, my point was - undeniably faster, but how much so...  Anyone got
                            > numbers?[/color]
                            In large numbers, it is significant. Ran under Kubuntu Linux with PHP 5.1.2
                            with 1GB RAM and 3.2Ghz processor:

                            Output:
                            S Diff: 11.428... (single quotes)
                            D Diff: 19.623... (double quotes)

                            Script:
                            <?php
                            $s_start = microtime(true) ;

                            for ($x = 0; $x < 99999; $x++)
                            {
                            echo 'Hello. How are you?<br />';
                            }

                            $s_end = microtime(true) ;

                            $d_start = microtime(true) ;

                            for ($x = 0; $x < 99999; $x++)
                            {
                            echo "Hello. How are you?<br />";
                            }

                            $d_end = microtime(true) ;

                            echo 'S Diff: ' . ($s_end - $s_start) . '<br />';
                            echo 'D Diff: ' . ($d_end - $d_start) . '<br />';
                            ?>


                            So, single quotes wins hands down. To compare echo vs print:

                            Output:
                            E Diff: 12.626... (echo)
                            P Diff: 15.637... (print)

                            <?php
                            $e_start = microtime(true) ;

                            for ($x = 0; $x < 99999; $x++)
                            {
                            echo 'Hello. How are you?<br />';
                            }

                            $e_end = microtime(true) ;

                            $p_start = microtime(true) ;

                            for ($x = 0; $x < 99999; $x++)
                            {
                            print 'Hello. How are you?<br />';
                            }

                            $p_end = microtime(true) ;

                            echo 'E Diff: ' . ($e_end - $e_start) . '<br />';
                            echo 'P Diff: ' . ($p_end - $p_start) . '<br />';
                            ?>

                            The fastest is echo with single quotes.

                            --
                            Carl Vondrick
                            Web-Engineer
                            Carl Vondrick is a Professor of Computer Science at Columbia University, researching computer vision, machine learning, and AI applications.

                            Comment

                            • Mladen Gogala

                              #15
                              Re: Newline

                              On Thu, 13 Apr 2006 10:22:12 -0700, infernon wrote:
                              [color=blue]
                              > Is this correct practice?[/color]

                              print "<br>";



                              --
                              大红鹰娱乐平台采用顶级加密技术,确保用户数据与交易的安全无虞,让玩家能够放心畅玩。


                              Comment

                              Working...