0

Hi!

I am kind of new to php.

$myFile = "msg.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $beforeData;
fwrite($fh, $stringData);
$stringData = ""+myname+": "+mymessage+"\n";
fwrite($fh, $stringData);
fclose($fh);

I need to set more than one variable in that red line. How do I do that?

I'm trying to make a chat. It writes in the TXT file. How do I set 2 vars in one line?

This doesn't work:
$stringData = ""+$myname+": "+$mymessage+"\n";

Please help! Thanks!

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by sawmaster
0

in PHP you use a period to concatenate/join strings:

$stringData = $myname . ": ". $mymessage . "\n";
0

Correctly posted above!

Another way would be to do the following:

// Sets $stringData to contain $myname.
$stringData = $myname;
// Takes whatever is in $stringData and adds a semi-colon and a space.
$stringData .= ": ";
// Takes everything so far and then adds the message, followed by a newline.
$stringData .= $mymessage . "\n";

The previous way posted above is just as good, and infact I prefer it, but I thought I'd show you this way too just so you know that you can concatenate strings like this.

Anthony

0

Hi!

Thanks! :) My code should work now! Currently, my computer is freezing up so I can't try it now. I have to restart. Thanks again!

Bye!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.