...

View Full Version : Why can't I switch new line with "\n"?



jayyu317
12-30-2007, 01:01 AM
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

oesxyl
12-30-2007, 01:04 AM
'\n' are ignored in html, you must use "<br>" in html and "<br/>" in xhtml.


best regards

jayyu317
12-30-2007, 01:14 AM
It works! Thank you very much!

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

oesxyl
12-30-2007, 01:18 AM
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

jayyu317
12-30-2007, 01:38 AM
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!

oesxyl
12-30-2007, 02:42 AM
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

dumpfi
12-30-2007, 10:42 AM
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

jayyu317
12-30-2007, 08:27 PM
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!

oesxyl
12-30-2007, 08:31 PM
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

jayyu317
12-30-2007, 08:38 PM
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?

jayyu317
12-30-2007, 08:40 PM
BTW, is there any tool on the page to show my post is solved. I can not find the place to do it.

oesxyl
12-30-2007, 09:01 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum