tags:

views:

74

answers:

2

What is .= in PHP? Also, I know in JavaScript, you can use += to add strings together, but can you do that in PHP?

+5  A: 

Of course you can, .= operator in PHP is the Java/JavaScript equivalent of +=, which can be interpreted to, append another variable with itself and assign it back to itself.

$var1 .= $var2

It's equivalent to,

$var1 = $var1 . $var2;

From PHP: String Operators manual along with the provided example,

<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>

The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.

Anthony Forloney
equivalent to `+` or equivalent to `.`?
dreamlax
@dreamlax, Thanks for noticing, I am currently in my Java mode, had to switch over.
Anthony Forloney
+1  A: 

.= appends the right side of the .= to the left side.

$something = "hello";
$something .= ", world!";

echo $something; // prints "hello, world!"
dreamlax