Jump to content

Archived

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

jeffpoulsen

New Line Problem

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.

Share this post


Link to post
Share on other sites
your not [b]appending[/b] the newline char.

In php, to "add strings to strings" you must use the [b].[/b] (fullstop) operator.

eg:
[code]$hello = "Hello" . " im Heckenschutze";[/code]

so in your case:
[code]fwrite($fh, $today . "\n")  [/code]

hth

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
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 :|

Share this post


Link to post
Share on other sites

×

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.