What is .= in php?

Asked

Viewed 320 times

13

What is .= in PHP? I’m wondering if it’s a concatenation or multiplication.

  • See here: http://www.php.net/manualen/language.operators.assignment.php

  • Thank you! My fault.

  • Better still: http://stackoverflow.com/questions/2202331/what-is-the-difference-between-and-in-php

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

4 answers

12

It serves to concatenate strings. It adds new text to a text already exists in the variable.

$texto .= "um texto aqui";

is the same as

$texto = $texto . "um texto aqui".

Another example:

$texto = "";
for ($i = 0; $i < 10; $i++) {
    $texto .= $i . " ";
}
echo $texto;

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I found a answer in the OS with a curiosity about the performance of the use of concatenation and construction of string with specialized classes. In PHP the concatenation is very cheap because the string is changeable. In C#, because it is changeless, it costs expensive if you do it in large proportion, then the StringBuilder goes much better when you need this pattern in large amount. But note that using it right, in C# the performance is much better, as was to be expected.

Multiplication would be

$valor *= 2;

If this is in a loop, it will double the value each passage of the loop.

9

Is the same as += in other languages. Since PHP is not typed, .= is used to strings, it adds the text to the right of the existing text in string.

$texto = "Hello, ";
$texto .= "World";
echo($texto); 

//Saída > Hello, World

5


.= is a leaner way of concatenating without having to rewrite the variable:

Would be similar to this: $variavel = $variavel . "sua string concatenada";

In this situation, you are saying that the variable will receive something new that will concatenate with what it already has.

That is to say:

$variavel = "texto";

$variavel .=" novo";

echo $variavel;

The exit from $variavel will be: "texto novo"

This rule also applies for calculations:

  • += (sum) => Example: $soma = 2; $soma+=2;the exit of $soma will be 4;
  • -= (subtraction)
  • /= (division)
  • *= (multiplication)

Tip: I know two ways to make concatenations in a row, one of them is this:

$variavel = "letras: A,";
$variavel .= "B,";
$variavel .= "C";

Another is using array:

$variavel = [];
$variavel[] = "letras: A";
$variavel[] = "B";
$variavel[] = "C";

echo implode(",", $variavel);

3

I would say that .= is a form of contraction (a shortening) to the concatenation syntax in order to make it simpler.

In PHP, the operator of . is responsible for the concatenation of strings.

Example:

$ab = 'a' . 'b'

var_dump($ab); // ab

However, the operator .= will make a concatenation of a string combining it with the value already existing in the variable.

$ab = 'a';

$ab .= 'b';

var_dump($ab); // 'ab'

Thus, use the operator .= makes operation easier in some cases than if using the ..

Example:

$texto = "o rato roeu"

$texto .= " a roupa do rei de Roma"

If you were to use the operator ., your code would get a little bigger:

$texto = "o rato roeu"

$texto = $texto . " a roupa do rei de Roma"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.