Page 1 of 1

Newbie question about linefeeds

Posted: Mon Oct 04, 2004 4:23 pm
by burn
Hello everybody I just registered to the forums! Woah! This place is huge, I think it'll be my new home :D

I would like some help with the line feed and carriage return characters. What I'm trying to do is output some text files to create an application error log. I didn't even start trying to append lines to the log when I realized I couldn't manage to write new lines.. This is the generic test code I wrote:

Code: Select all

$content = $db_day.$db_month.substr($db_year, 2, 2).'\n';
$content .= $dbf_day.$dbf_month.substr($dbf_year, 2, 2).'\n';
$content .= $priorita .'\n';
$content .= $record['titolo'].'\n';
$content .= $record['testo'].'\n';

$handle = fopen($filename, 'w'));
fwrite($handle, $content);

fclose($handle);
What I get in the file is one long line containing the information outputed to the file separated by "\n"s.. Is this supposed to happen? Or there's some configuration to my system that I should take care of? Obsiously I tried various combination to match systems compatibility (Mac \r Win \n\r) and I keep getting the same results, instead of the special chars I get printed to the file the literal \n..

It's very unclear to me at which level the system should translate those into what I mean to be a carriage return or a new line, if not both.

I develop over a Gentoo box with php4 and apache 2.

Thanks in advance for your inputs guys,
stay cool!

Burn

Posted: Mon Oct 04, 2004 4:32 pm
by feyd
tip: use

Code: Select all

tags when posting something with php code in it.. we'll receive it much more kindly.

you want to use

Code: Select all

"\n"
not single quoted strings.. single quoted strings don't escape a lot of things, including variables, line breaks (in any form) and other characters like the tab metacharacter.

Posted: Mon Oct 04, 2004 4:38 pm
by burn
This was very enlightening :idea: , I think I have to go through a major revision of my standard code writing methods.

Thanks for the great input, and for pointing out the PHP tag, it looks much better indeed!

And my compliments for such an outstanding community!