1
votes

Take this example:

<?php

$i = 1;
$i += 2;
$i =+ 5;

echo $i; // 5

This shows that =+ is an assignment operator. Still, this is highly confusing to me and not at all semantic. I spent hours debugging something simply because I accidentally used =+ instead of +=. The former does not throw errors, though. So I am curious: what is the use case for =+. When would you ever (need to) use it over a simple =?

3

3 Answers

3
votes

=+ is not an operator. Think of it as

$i = +5;

as opposed to

$i = -5;
0
votes

=+ is not a single operator. Its two: the assignment (=) and the unary plus (+).

If you insert different whitespace it gets obvious:

$i = +5;

The unary plus in PHP is used for conversion of $i to int or float as appropriate. Since 5 already is an int, its the identity in this case, and the whole expression is semantically equivalent to:

 $i = 5;
0
votes

As you mentioned += is an assignment operator is correct.

In your case:

$i = 1;

Just assign the value 1 to $i and then when your second line execute $i += 2;, its just add the value 2 with the existing value of $i.

So now when you use $i =+ 5; in your third line, its re-assign the variable $i with the positive value +5.

There are no big deal with =+ and += these are different.