tags:

views:

102

answers:

5

PHP: what is the difference between $varName = "$var \n"; from that with a period: $varName. = "$var \n"? quite confusing.

+1  A: 

= is for attributing a value
.= is for concatenating

$var = "a";
$var = "b"; // $var will be "b";

$var = "a";
$var .= "b"; // $var will be "ab";
Timotei Dolean
+10  A: 

The . operator in PHP means concatenation.

You can use operators with the assignment operator (=) to accomplish both affects.

So these are the same:

$varname .= "stuff";
$varname = $varname . "stuff";

Which basically means, the original value plus the new value.

seanmonstar
+3  A: 

Also note that the same syntax applies to every mathematical operators such as + - / * %.

eg :

$i = 1;
$i += 1;
echo $i; // outputs 2
Sylvain
A: 

i believe it is just to append to the existing string. ie:

$str = "i went to";
$str .= "the park";
//$str will be "i went to the park"
Actually, $str will be "i went tothe park". You missed a space in there :-)
Sander Marechal
A: 

in this instance, they both achieve the same effect.
however, $varName = "$var \n" initialises $varName to "$var \n"
whereas, $varName. = "$var \n" initialises $varname to '' (empty) and appends "$var \n" to the end