Newbie question about linefeeds

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
burn
Forum Newbie
Posts: 2
Joined: Mon Oct 04, 2004 3:47 pm
Location: Cortona, Italy
Contact:

Newbie question about linefeeds

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
burn
Forum Newbie
Posts: 2
Joined: Mon Oct 04, 2004 3:47 pm
Location: Cortona, Italy
Contact:

Post 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!
Post Reply