Jump to content

New Line Problem


jeffpoulsen

Recommended Posts

I'm having trouble getting the new line \n to work in a simple fwrite line. The code is below:

fwrite($fh,$today)      This Works
fwrite($fh,$today"\n")  This does not 

These line are straight out of the book I'm using to learn PHP. I have read about using <br> instead but can't get that to work either.
Link to comment
https://forums.phpfreaks.com/topic/24922-new-line-problem/
Share on other sites

[quote author=jeffpoulsen link=topic=112522.msg456741#msg456741 date=1161689379]
I'm having trouble getting the new line \n to work in a simple fwrite line. The code is below:

fwrite($fh,$today)      This Works
fwrite($fh,$today"\n")  This does not 

These line are straight out of the book I'm using to learn PHP. I have read about using <br> instead but can't get that to work either.
[/quote]

you need to seperate the quotes and the variable. either way here will do:
[code]
fwrite($fh,$today."\n")  // does now!
fwrite($fh,"$today\n")  // does now!
[/code]
Link to comment
https://forums.phpfreaks.com/topic/24922-new-line-problem/#findComment-113586
Share on other sites

Thanks for the help. It's hard to learn when even the books you use are wrong.

Getting a little  closer but not there yet. I have tried the above codes and now it doesn't kill the script but what the object the lesson was to write to a flat file and have data go on a new line each time. None of these do that. The entire code for this is below. When I check the file /dwlog.txt it shows the date sent on one line.

                $today = date('M j g:i a');
$fh = fopen("dwlog.txt","a");
fwrite($fh,$today . "\n");
fclose($fh);

By the way  fwrite($fh,"$today\n");  did not work.
I can see learning PHP is going to be tough.
Link to comment
https://forums.phpfreaks.com/topic/24922-new-line-problem/#findComment-113593
Share on other sites

Your only writing the date once... the file will look like

[code]DATE (NEWLINE)
|[/code]

eg 2 lines:
[code]      $today = date('M j g:i a');
      $fh = fopen("dwlog.txt","a");
      fwrite($fh,$today . "\n");// line 1
      fwrite($fh,$today . "\n");// line 2
      fclose($fh);[/code]

hth :|
Link to comment
https://forums.phpfreaks.com/topic/24922-new-line-problem/#findComment-113603
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.