Newline (\n) in printf doesn't work?

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

    Newline (\n) in printf doesn't work?

    I'm new to PHP.

    I see that PHP supports the C printf function, and I've seen examples
    like printf("Hello world!\n"); however the newline character \n
    doesn't work - i.e., it does not generate an HTML <br>, which I would
    have expected - what it does is generate a newline in the html
    generated text, but since the browser ignores blank lines, this
    feature appears useless for most applications.
    >From this arises two questions:
    1. I am misunderstandin g something?

    2. What is the prescribed way of inserting a newline into a PHP
    string? (Could it be printf("Hello world! <br>") ?

    Thanks,
    M. McDonnell

  • Jerry Stuckle

    #2
    Re: Newline (\n) in printf doesn't work?

    Michael wrote:
    I'm new to PHP.
    >
    I see that PHP supports the C printf function, and I've seen examples
    like printf("Hello world!\n"); however the newline character \n
    doesn't work - i.e., it does not generate an HTML <br>, which I would
    have expected - what it does is generate a newline in the html
    generated text, but since the browser ignores blank lines, this
    feature appears useless for most applications.
    >
    >>From this arises two questions:
    >
    1. I am misunderstandin g something?
    >
    2. What is the prescribed way of inserting a newline into a PHP
    string? (Could it be printf("Hello world! <br>") ?
    >
    Thanks,
    M. McDonnell
    >
    It works fine. It generates a nl character, just as you asked it to.
    Look at your source code - you'll see the line break.

    If you want an HTML <bror xthtml <br />, you need to code them
    yourself. PHP does not convert for you.

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

    Comment

    • Michael

      #3
      Re: Newline (\n) in printf doesn't work?

      On May 27, 8:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      Michael wrote:
      I'm new to PHP.
      >
      I see that PHP supports the C printf function, and I've seen examples
      like printf("Hello world!\n"); however the newline character \n
      doesn't work - i.e., it does not generate an HTML <br>, which I would
      have expected - what it does is generate a newline in the html
      generated text, but since the browser ignores blank lines, this
      feature appears useless for most applications.
      >
      >From this arises two questions:
      >
      1. I am misunderstandin g something?
      >
      2. What is the prescribed way of inserting a newline into a PHP
      string? (Could it be printf("Hello world! <br>") ?
      >
      Thanks,
      M. McDonnell
      >
      It works fine. It generates a nl character, just as you asked it to.
      Look at your source code - you'll see the line break.
      >
      If you want an HTML <bror xthtml <br />, you need to code them
      yourself. PHP does not convert for you.
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===- Hide quoted text -
      >
      - Show quoted text -
      Jerry,

      Thanks for your reply. Yes, I looked at the source code, but no <br>,
      all I get is a new line of white space, which as we know is ignored by
      the browser.

      Comment

      • rf

        #4
        Re: Newline (\n) in printf doesn't work?


        "Michael" <MichaelDMcDonn ell@yahoo.comwr ote in message
        news:1180319398 .779675.292260@ j4g2000prf.goog legroups.com...
        I'm new to PHP.
        >
        I see that PHP supports the C printf function, and I've seen examples
        like printf("Hello world!\n"); however the newline character \n
        doesn't work - i.e., it does not generate an HTML <br>, which I would
        have expected - what it does is generate a newline in the html
        generated text, but since the browser ignores blank lines, this
        feature appears useless for most applications.
        >
        >>From this arises two questions:
        >
        1. I am misunderstandin g something?
        >
        2. What is the prescribed way of inserting a newline into a PHP
        string? (Could it be printf("Hello world! <br>") ?
        Yes.

        More properly

        printf("Hello world! <br>\r\n");

        so you get a line break in the source HTML as well, making it easier to
        read.

        or, since you are not actually formatting anything:

        print("Hello world! <br>\r\n");

        or even

        echo "Hello world! <br>\r\n";

        --
        Richard.


        Comment

        • Chris Hope

          #5
          Re: Newline (\n) in printf doesn't work?

          Michael wrote:
          On May 27, 8:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
          >Michael wrote:
          I'm new to PHP.
          >>
          I see that PHP supports the C printf function, and I've seen
          examples like printf("Hello world!\n"); however the newline
          character \n doesn't work - i.e., it does not generate an HTML
          <br>, which I would have expected - what it does is generate a
          newline in the html generated text, but since the browser ignores
          blank lines, this feature appears useless for most applications.
          >>
          >>From this arises two questions:
          >>
          1. I am misunderstandin g something?
          >>
          2. What is the prescribed way of inserting a newline into a PHP
          string? (Could it be printf("Hello world! <br>") ?
          >>
          >It works fine. It generates a nl character, just as you asked it to.
          >Look at your source code - you'll see the butline break.
          >>
          >If you want an HTML <bror xthtml <br />, you need to code them
          >yourself. PHP does not convert for you.
          >
          Thanks for your reply. Yes, I looked at the source code, but no <br>,
          all I get is a new line of white space, which as we know is ignored by
          the browser.
          That's correct. You didn't ask PHP to write a <brtag. You asked PHP to
          write a newline break, which is exactly what it did.

          So yes, your question 2) is what you would need to do, although your
          question should read like this:

          2. What is the prescribed way of inserting an *html* *newline* break*
          *tag* into a PHP string? (Could it be printf("Hello world! <br>") ?

          There is also the nl2br() function which converts "\n" to "<br />\n"
          should you need to automatically need to do this with a string.

          --
          Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

          Comment

          • Michael

            #6
            Re: Newline (\n) in printf doesn't work?

            On May 27, 8:20 pm, Chris Hope <blackh...@elec trictoolbox.com wrote:
            Michael wrote:
            On May 27, 8:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            Michael wrote:
            I'm new to PHP.
            >
            I see that PHP supports the C printf function, and I've seen
            examples like printf("Hello world!\n"); however the newline
            character \n doesn't work - i.e., it does not generate an HTML
            <br>, which I would have expected - what it does is generate a
            newline in the html generated text, but since the browser ignores
            blank lines, this feature appears useless for most applications.
            >
            >From this arises two questions:
            >
            1. I am misunderstandin g something?
            >
            2. What is the prescribed way of inserting a newline into a PHP
            string? (Could it be printf("Hello world! <br>") ?
            >
            It works fine. It generates a nl character, just as you asked it to.
            Look at your source code - you'll see the butline break.
            >
            If you want an HTML <bror xthtml <br />, you need to code them
            yourself. PHP does not convert for you.
            >
            Thanks for your reply. Yes, I looked at the source code, but no <br>,
            all I get is a new line of white space, which as we know is ignored by
            the browser.
            >
            That's correct. You didn't ask PHP to write a <brtag. You asked PHP to
            write a newline break, which is exactly what it did.
            >
            So yes, your question 2) is what you would need to do, although your
            question should read like this:
            >
            2. What is the prescribed way of inserting an *html* *newline* break*
            *tag* into a PHP string? (Could it be printf("Hello world! <br>") ?
            >
            There is also the nl2br() function which converts "\n" to "<br />\n"
            should you need to automatically need to do this with a string.
            >
            --
            Chris Hope |www.electricto olbox.com|www.linuxcdmall.com- Hide quoted text -
            >
            - Show quoted text -
            Thank you all for your help. I now understand all of this a bit
            better.

            Comment

            • Tim Roberts

              #7
              Re: Newline (\n) in printf doesn't work?

              Michael <MichaelDMcDonn ell@yahoo.comwr ote:
              >
              >I'm new to PHP.
              >
              >I see that PHP supports the C printf function, and I've seen examples
              >like printf("Hello world!\n"); however the newline character \n
              >doesn't work - i.e., it does not generate an HTML <br>, which I would
              >have expected - what it does is generate a newline in the html
              >generated text, but since the browser ignores blank lines, this
              >feature appears useless for most applications.
              What about <preor <textarearegion s? What if I care about the way my
              generated HTML looks, and I want things to line up neatly?
              --
              Tim Roberts, timr@probo.com
              Providenza & Boekelheide, Inc.

              Comment

              • Jerry Stuckle

                #8
                Re: Newline (\n) in printf doesn't work?

                Tim Roberts wrote:
                Michael <MichaelDMcDonn ell@yahoo.comwr ote:
                >I'm new to PHP.
                >>
                >I see that PHP supports the C printf function, and I've seen examples
                >like printf("Hello world!\n"); however the newline character \n
                >doesn't work - i.e., it does not generate an HTML <br>, which I would
                >have expected - what it does is generate a newline in the html
                >generated text, but since the browser ignores blank lines, this
                >feature appears useless for most applications.
                >
                What about <preor <textarearegion s? What if I care about the way my
                generated HTML looks, and I want things to line up neatly?
                The first rule of HTML is - make your layout fluid! Allow it to adjust
                to the size of the user's browser window.

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

                Comment

                • Tim Roberts

                  #9
                  Re: Newline (\n) in printf doesn't work?

                  Jerry Stuckle <jstucklex@attg lobal.netwrote:
                  >Tim Roberts wrote:
                  >Michael <MichaelDMcDonn ell@yahoo.comwr ote:
                  >>I'm new to PHP.
                  >>>
                  >>I see that PHP supports the C printf function, and I've seen examples
                  >>like printf("Hello world!\n"); however the newline character \n
                  >>doesn't work - i.e., it does not generate an HTML <br>, which I would
                  >>have expected - what it does is generate a newline in the html
                  >>generated text, but since the browser ignores blank lines, this
                  >>feature appears useless for most applications.
                  >>
                  >What about <preor <textarearegion s? What if I care about the way my
                  >generated HTML looks, and I want things to line up neatly?
                  >
                  >The first rule of HTML is - make your layout fluid! Allow it to adjust
                  >to the size of the user's browser window.
                  What I meant is, "what if I want my HTML code to line up neatly?" For
                  example:
                  <select name="City">
                  <option>Alban y</option>
                  <option>Portlan d</option>
                  <option>Salem </option>
                  <option>Tigar d</option>
                  </select>

                  The browser would be perfectly happy with one immensely long line, of
                  course, but it's not unreasonable for me to want the HTML to look like the
                  above, and that requires printing \n newlines.
                  --
                  Tim Roberts, timr@probo.com
                  Providenza & Boekelheide, Inc.

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Newline (\n) in printf doesn't work?

                    Tim Roberts wrote:
                    Jerry Stuckle <jstucklex@attg lobal.netwrote:
                    >
                    >Tim Roberts wrote:
                    >>Michael <MichaelDMcDonn ell@yahoo.comwr ote:
                    >>>I'm new to PHP.
                    >>>>
                    >>>I see that PHP supports the C printf function, and I've seen examples
                    >>>like printf("Hello world!\n"); however the newline character \n
                    >>>doesn't work - i.e., it does not generate an HTML <br>, which I would
                    >>>have expected - what it does is generate a newline in the html
                    >>>generated text, but since the browser ignores blank lines, this
                    >>>feature appears useless for most applications.
                    >>What about <preor <textarearegion s? What if I care about the way my
                    >>generated HTML looks, and I want things to line up neatly?
                    >The first rule of HTML is - make your layout fluid! Allow it to adjust
                    >to the size of the user's browser window.
                    >
                    What I meant is, "what if I want my HTML code to line up neatly?" For
                    example:
                    <select name="City">
                    <option>Alban y</option>
                    <option>Portlan d</option>
                    <option>Salem </option>
                    <option>Tigar d</option>
                    </select>
                    >
                    The browser would be perfectly happy with one immensely long line, of
                    course, but it's not unreasonable for me to want the HTML to look like the
                    above, and that requires printing \n newlines.
                    What does this have to do with converting newline characters to <br>?
                    Newline characters will affect the generated html, <brwon't.

                    But if you really want to bloat your html with lots of extra white
                    space, increasing the amount of bandwidth required to load your pages
                    and slow down your customers, by all means, do it.

                    Now I'm not suggesting you have no formatting - but you can overdo it, also.

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

                    Comment

                    • Tim Roberts

                      #11
                      Re: Newline (\n) in printf doesn't work?

                      Jerry Stuckle <jstucklex@attg lobal.netwrote:
                      >
                      >What does this have to do with converting newline characters to <br>?
                      >Newline characters will affect the generated html, <brwon't.
                      Nothing. That's not what this thread was about. The original poster
                      opined that the ability to print \n newlines was useless. I disagreed.
                      --
                      Tim Roberts, timr@probo.com
                      Providenza & Boekelheide, Inc.

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: Newline (\n) in printf doesn't work?

                        Tim Roberts wrote:
                        Jerry Stuckle <jstucklex@attg lobal.netwrote:
                        >What does this have to do with converting newline characters to <br>?
                        >Newline characters will affect the generated html, <brwon't.
                        >
                        Nothing. That's not what this thread was about. The original poster
                        opined that the ability to print \n newlines was useless. I disagreed.
                        Well, I can agree with him that with html, \n newlines are useless.

                        But you cut off the more important part of my post. Just to remind you,
                        it was:

                        <quote>
                        But if you really want to bloat your html with lots of extra white
                        space, increasing the amount of bandwidth required to load your pages
                        and slow down your customers, by all means, do it.

                        Now I'm not suggesting you have no formatting - but you can overdo it,
                        also.
                        </quote>


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

                        Comment

                        Working...