PHP newline

Ampsonic

Gawd
Joined
Apr 18, 2004
Messages
830
Delving into the world of PHP for the first time. I'm trying to do some basic file output stuff, here's my code.

Code:
<?php
$file = fopen("test.txt", "a");
fwrite($file,$comment);
?>

Works fine, except I can't get it to insert a newline. Any attempt at escape characters are ignored and put into the file.

Thanks,
Nick
 
That piece of code looks like it should work but you dont show us how you're initializing $comment.

Code:
$comment = "hello\nworld";

also, depending on your platform, you may need to use

Code:
$comment = "hello\r\nworld";

also, if you're trying to view the file via a webbrowser, you may need to use <br /> for your breaks instead...but since you're outputing to txt, i doubt this is the problem.

--KK
 
Thanks, got it now, i think the problem is that i was using ' instead of " to surround my \n


Nick
 
That is exactly why your code failed, ' " and ` (single-quote, double quote, and backtick) all have different semantic meanings for a piece of code. Oreilly's PHP book can give you the gory details, but it is largely inheirited from perl's quoting syntax.
 
Back
Top