Page 1 of 1

Strange New-Line problem....

Posted: Thu Jul 07, 2005 11:52 am
by IceMetalPunk
Hello everyone. I am new to PHP (just started learning it yesterday), but it seems fairly easy (maybe because I am familiar with a few other programming languages). However, I have come upon a strange problem. In everything I have read about PHP, \n is a new line, or \r\n can work as well. However, it doesn't work for me. I am trying to write a text file on the server. When I use \n, it writes a strange character to the file. When I use \r\n, it does start a new line, but it also indents the new line, which I don't want. Here's the code I'm using:

Code: Select all

<html>
<body>

<?php
if (!($f=fopen("welcome.txt","a+"))) {
echo "Unable to open file!"; 
}
else {
if (!fwrite($f,"$id \r\n Hello")) {
echo "Unable to write information to PHP file.";
}
else {
echo "Done! Please click <a href='history.go(-1)'>here</a> to return to the previous page.";
}
fclose($f);
}
?>

</body>
</html>
the new line is in the fwrite part right above the "Unable to write information..." line. Am I doing something wrong, or is there no way to do this?

-IMP ;) :)

*EDIT* The blue "are" at the top is supposed to be the letter "R" (lower-case), but the forums are replacing it.

Posted: Thu Jul 07, 2005 12:01 pm
by pickle
If you're running on a Linux server, the newline character is "\n". If you're running on Windows, it's "\r\n" (the 'r is, or course, the lower case letter).

It seems the reason the next line is indented, is because you've put a space between the newline character and "Hello". Take that space out and it should work fine.

Posted: Thu Jul 07, 2005 12:04 pm
by IceMetalPunk
THANKS! I didn't realize I had the extra space in there! Again, thank you so much!

-IMP ;) :)