What does the .= operator mean in PHP?

50,084

Solution 1

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.

Solution 2

This is for concatenation

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

echo $var; // this will give you testvalue

Solution 3

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".

Share:
50,084

Related videos on Youtube

codacopia
Author by

codacopia

Codacopia is a website development company specializing in Wordpress development. Whether it is a quick fix and consultation needed or full site redesign, we've got ya covered. Are you looking to build your marketing efforts online? If so that's what we do best, so let's get in touch. Vist us at codacopia.com

Updated on January 22, 2020

Comments

  • codacopia
    codacopia over 4 years

    I have a variable that is being defined as

    $var .= "value";
    

    How does the use of the dot equal function?

    • Deadlock
      Deadlock about 11 years
      Used to append value in variable which already contains some value...
  • Marc Baumbach
    Marc Baumbach about 11 years
    +1 I'm not sure why there was a down vote either. This is also backed by php.net/manual/en/language.operators.string.php
  • m93a
    m93a about 9 years
    Both do re-assign $x.
  • Blender
    Blender about 9 years
    @m93a: Can you link to documentation?
  • m93a
    m93a about 9 years
    @Blender OK, you're right, they don't. The $x .= performs ~2 times faster than $x = $x . but the spec isn't really verbose on this matter.
  • Radmation
    Radmation over 5 years
    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!
  • Camille Goudeseune
    Camille Goudeseune about 5 years
    Do you mean that $x = "Hi"; $pi = 3.14; $x .= $pi; changes $pi?!