Jump to content


Photo

New Line Problem


  • Please log in to reply
6 replies to this topic

#1 jeffpoulsen

jeffpoulsen
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 24 October 2006 - 11:29 AM

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.

#2 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 24 October 2006 - 11:37 AM

your not appending the newline char.

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

eg:
$hello = "Hello" . " im Heckenschutze";

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

hth

#3 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 24 October 2006 - 11:38 AM

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.


you need to seperate the quotes and the variable. either way here will do:
fwrite($fh,$today."\n")  // does now!
fwrite($fh,"$today\n")  // does now!

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#4 jeffpoulsen

jeffpoulsen
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 24 October 2006 - 12:09 PM

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.

#5 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 24 October 2006 - 12:32 PM

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

DATE (NEWLINE)
|

eg 2 lines:
      $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);

hth :|


#6 jeffpoulsen

jeffpoulsen
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 24 October 2006 - 12:58 PM

Thanks Heckenschutze




#7 True`Logic

True`Logic
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 24 October 2006 - 03:43 PM

fwrite($fh,$today . "\n");
or
fwrite($fh,"$today\n");[code]

work[/code]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users