Bram Vanroy Bram Vanroy - 1 year ago 42
PHP Question

Validity and usage =+ in PHP

Take this example:


$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

Answer Source

=+ 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;