Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Why can't I switch new line with "\n"?

    I am new learner of php. When I use the following script to echo several lines records, they are in one line. What is the reason? Thanks very much!

    <?php
    $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');

    reset($fruit);
    while (list($key, $val) = each($fruit)) {
    echo "$key => $val\n";
    }
    ?>

    It shows:
    a => apple b => banana c => cranberry.


    According to php manual, it should be:

    a => apple
    b => banana
    c => cranberry

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    '\n' are ignored in html, you must use "<br>" in html and "<br/>" in xhtml.


    best regards

  • Users who have thanked oesxyl for this post:

    jayyu317 (12-30-2007)

  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It works! Thank you very much!

    But I can use both <br/> and <br> in html. I don't why.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by jayyu317 View Post
    It works! Thank you very much!

    But I can use both <br/> and <br> in html. I don't why.
    only one kind of tag is correct depending of what your output is, html or xhtml
    <br> is the tag for html
    <br/> same tag but for xhtml

    best regards

  • #5
    New Coder
    Join Date
    Oct 2007
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I have the same question when I write records to txt file. I want to use following script

    $outputstring = $date."\t".$tireqty." tires\t".$oilqty." oil\t" $sparkqty." spark plugs\t\$".$total ."\t". $address."\n";
    $fp = fopen("orders.txt", 'a');
    flock($fp, LOCK_EX);
    fwrite($fp, $outputstring);
    flock($fp, LOCK_UN);
    fclose($fp);

    to add records to orders.txt file and show records as this way:

    15:42, 20th April 4 tires 1 oil 6 spark plugs $434.00 22 Short St, Smalltown
    15:43, 20th April 1 tires 0 oil 0 spark plugs $100.00 33 Main Rd, Newtown


    But after adding, they are in one line:
    15:42, 20th April 4 tires 1 oil 6 spark plugs $434.00 22 Short St, Smalltown15:43, 20th April 1 tires 0 oil 0 spark plugs $100.00 33 Main Rd, Newtown

    I replaced above script with . $address."<br>". But it can not work and show "<br>" between records.

    How can I show the records line by line when writing in the txt file?

    Thank!

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    you must use the correct 'newline' and that depend of your operating system and the output.
    For text file there are:
    - '\n' for unix-like sistem
    - '\r' for mac, AFAIK
    - '\r\n' for windows

    '<br>' and '<br/>' are only for web pages.

    best regards

  • #7
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    If your version of PHP is >= 4.3.10 or >= 5.0.2 you can use the constant PHP_EOL to get the correct newline for your OS.

    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • Users who have thanked dumpfi for this post:

    Bobafart (01-04-2008)

  • #8
    New Coder
    Join Date
    Oct 2007
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I use \n to write the txt file now, but read that txt file with wordpad or notepad2. It can show the new line now. Notepad do not support the \n when reading txt file, I think. Thanks all!

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by jayyu317 View Post
    I use \n to write the txt file now, but read that txt file with wordpad or notepad2. It can show the new line now. Notepad do not support the \n when reading txt file, I think. Thanks all!
    for notepad you can use "\r\n".

    best regards

  • Users who have thanked oesxyl for this post:

    jayyu317 (12-30-2007)

  • #10
    New Coder
    Join Date
    Oct 2007
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    That is good for notepad, but if I want to read that txt file with other editor when using \r\n, can other editor read it?

  • #11
    New Coder
    Join Date
    Oct 2007
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    BTW, is there any tool on the page to show my post is solved. I can not find the place to do it.

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by jayyu317 View Post
    That is good for notepad, but if I want to read that txt file with other editor when using \r\n, can other editor read it?
    you can read with any editor, but if you write back to disk others editors then notepad can mess the file with format chars.

    about closing post, I presume that you need to open a new thread with a new question and live this how it is, I mean don't post anymore if is closed,

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •