52

I have a variable that is being defined as

$var .= "value";

How does the use of the dot equal function?

Andrey
  • 2,503
  • 3
  • 30
  • 39
codacopia
  • 2,369
  • 9
  • 39
  • 58

4 Answers4

77

It's the concatenating assignment operator. It works similarly to:

$var = $var . "value";

$x .= differs from $x = $x . in that the former is in-place, but the latter re-assigns $x.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • 1
    +1 I'm not sure why there was a down vote either. This is also backed by http://php.net/manual/en/language.operators.string.php – Marc Baumbach Feb 13 '13 at 04:53
  • Both do re-assign `$x`. – m93a Apr 08 '15 at 07:42
  • @m93a: Can you link to documentation? – Blender Apr 09 '15 at 02:18
  • 3
    @Blender OK, you're right, they don't. The `$x .=` performs [~2 times faster](http://michal.grno.cz/trash/assignment.php) than `$x = $x .` but the spec isn't really verbose on this matter. – m93a Apr 09 '15 at 06:18
  • 4
    yes but only for strings - i just spent fuc*** hours wondering why the hell my float was getting turned into a freaking string! Apparently this damn operator will cast your damn floats to strings.. I am so annoyed right now! – Radmation Sep 25 '18 at 16:41
  • Do you mean that `$x = "Hi"; $pi = 3.14; $x .= $pi;` changes `$pi`?! – Camille Goudeseune Feb 20 '19 at 22:09
  • 1
    @CamilleGoudeseune I imagine you know the answer 4 years later, but for posterity: No, in your example, $x .= $pi would not change $pi to a string. However, $pi .= $x would change $pi to a string, (specifically a string that equals $pi .$x or "3.14Hi"). – snoski Mar 19 '23 at 17:36
25

This is for concatenation

$var  = "test";
$var .= "value";

echo $var; // this will give you testvalue
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
19

the "." operator is the string concatenation operator. and ".=" will concatenate strings.

Example:

$var = 1;
$var .= 20;

This is same as:

$var = 1 . 20;

the ".=" operator is a string operator, it first converts the values to strings; and since "." means concatenate / append, the result is the string "120".

talha2k
  • 24,937
  • 4
  • 62
  • 81
-1

In very plain language, what happens is that whatever is stored in each variable is converted to a string and then each string is placed into a final variable that includes each value of each variable put together.

I use this to generate a random variable of alpha numeric and special characters. Example below:

function generateRandomString($length = 64) {
    $characters = '0123456789-!@#$%^*()?:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = mb_strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

then to get the value of $randomString I assign a variable to the function like so

$key = generateRandomString();
echo $key;

What this does is pick a random character from any of the characters in the $characters variable. Then .= puts the result of each of these random "picks" together, in a new variable that has 64 random picks from the $characters string group called $randomString.

airider74
  • 390
  • 3
  • 14