rfc:concatenation_precedence

PHP RFC: Change the precedence of the concatenation operator

Introduction

It's been a long standing issue that an (unparenthesized) expression with '+', '-' and '.' evaluates left-to-right.

echo "sum: " . $a + $b;
 
// current behavior: evaluated left-to-right
echo ("sum: " . $a) + $b;
 
// desired behavior: addition and subtraction have a higher precendence
echo "sum :" . ($a + $b);

This RFC aims to change that behavior to be less error-prone and more intuitive.

Proposal

Currently the precedence of '.', '+' and '-' operators are equal. Any combination of these operators are simply evaluated left-to-right.

This is counter-intuitive though: you rarely want to add or subtract concatenated strings which in general are not numbers. However, given PHPs capability of seamlessly converting an integer to a string, concatenation of these values is desired.

Thus, the RFC proposes to give '.' an inferior precedence to '+' and '-', so that additions and subtractions are always performed before the concatenation. Concretely, the new precedence will be right below '«' and '»' operators, given them also being “math” operators and not usable with non-numeric strings.

Backward Incompatible Changes

Every unparenthesized expression featuring an '-' or '+' after a '.' will change behavior. As an example, the expression "3" . "5" + 7 will now be equal to “312” instead of previously 42.

While this is a subtle behavior change in that it will give different outputs without notice or warning, it is trivially possible to statically analyze the code and find all instances where this happens. As to my knowledge these occurrences are quite rare as it almost always is an error in the current form, rendering the impact minimal.

As Nikita mentioned on the mailing list: http://news.php.net/php.internals/105442 - the impact to existing open-source code is de facto invisible; all found occurrences are actual bugs. This is a strong indicator that the overall impact will also be very minimal.

Proposed PHP Version(s)

PHP 8, with a deprecation notice in PHP 7.4 upon encountering an unparenthesized expression containing an '.' before a '+' or '-'.

Vote

Voting started 2019-04-30 and ends 2019-05-14. The first (primary) voting requires a 2/3 majority. The second (secondary) voting requires a 50%+1 majority.

Change the precedence of the concatenation operator as proposed in PHP 8?
Real name Yes No
andrey (andrey)  
ashnazg (ashnazg)  
bishop (bishop)  
bwoebi (bwoebi)  
carusogabriel (carusogabriel)  
cpriest (cpriest)  
daverandom (daverandom)  
derick (derick)  
diegopires (diegopires)  
duncan3dc (duncan3dc)  
emir (emir)  
galvao (galvao)  
gasolwu (gasolwu)  
jasny (jasny)  
kalle (kalle)  
kelunik (kelunik)  
kguest (kguest)  
kinncj (kinncj)  
laruence (laruence)  
levim (levim)  
malukenho (malukenho)  
marcio (marcio)  
mcmic (mcmic)  
ocramius (ocramius)  
patrickallaert (patrickallaert)  
peehaa (peehaa)  
petk (petk)  
pmmaga (pmmaga)  
pollita (pollita)  
salathe (salathe)  
sammyk (sammyk)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
stas (stas)  
trowski (trowski)  
Final result: 31 4
This poll has been closed.
Emit a deprecation notice in PHP 7.4 if the RFC is accepted?
Real name Yes No
andrey (andrey)  
ashnazg (ashnazg)  
bishop (bishop)  
bwoebi (bwoebi)  
carusogabriel (carusogabriel)  
cpriest (cpriest)  
daverandom (daverandom)  
derick (derick)  
diegopires (diegopires)  
duncan3dc (duncan3dc)  
emir (emir)  
galvao (galvao)  
gasolwu (gasolwu)  
girgias (girgias)  
jasny (jasny)  
kalle (kalle)  
kelunik (kelunik)  
kguest (kguest)  
kinncj (kinncj)  
levim (levim)  
malukenho (malukenho)  
marcio (marcio)  
mcmic (mcmic)  
ocramius (ocramius)  
patrickallaert (patrickallaert)  
peehaa (peehaa)  
petk (petk)  
pmmaga (pmmaga)  
pollita (pollita)  
rasmus (rasmus)  
salathe (salathe)  
sammyk (sammyk)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
stas (stas)  
trowski (trowski)  
yunosh (yunosh)  
Final result: 30 7
This poll has been closed.

Patch

rfc/concatenation_precedence.txt · Last modified: 2019/05/16 16:05 by bwoebi