Strange New-Line problem....

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
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Strange New-Line problem....

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

THANKS! I didn't realize I had the extra space in there! Again, thank you so much!

-IMP ;) :)
Post Reply