php / php-src

The PHP Interpreter
https://www.php.net
Other
38.16k stars 7.75k forks source link

`=.` #16669

Closed foremtehan closed 3 hours ago

foremtehan commented 4 hours ago

Description

I was wondering, why don’t we have an operator that performs the opposite of the concatenating assignment operator .=? This new operator could prepend a value:

$a = " World";
$a =. "Hello"; // Result: "Hello World!"

What's the problem with this?

nielsdos commented 4 hours ago

This won't generalise well. The reason .= exists is because all other binary operators can be used in compound form as well, e.g. +=, -=, ... so it makes sense to have .= too. However, =+ and =- would be ambiguous, consider this: $a =- 3. Does this means "set $a to -3" or "subtract 3 from $a". It would be inconsistent to have =. as an exception.