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!

Recommended Answers

All 3 Replies

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

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

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

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.